explode() problem

Discussion in 'PHP' started by gilgalbiblewheel, Nov 14, 2007.

  1. #1
    The following is in my sql's where statement. It's exploding the words to individual letters instead of splitting phrases. How do I fix that?

    if (!empty($keyword2))
        {
            $myarray = explode(' ', $keyword2);
    		$i = 0;
    	    $whereclause .= " AND text_data LIKE '%".$value[0]."%'";
    		$i++;
            foreach($myarray as $value)
                {
                	$whereclause .= " OR text_data LIKE '%".$value[$i]."%'";
    				$i++;
                }
            $sql .= $whereclause;  
    		$i++;      
        }
    PHP:
     
    gilgalbiblewheel, Nov 14, 2007 IP
  2. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #2
    
    if (!empty($keyword2))
        {
            $myarray = explode(' ', $keyword2);
            $i = 0;
            $whereclause .= " AND text_data LIKE '%".$value[0]."%'";
            $i++;
            foreach($myarray as $value)
                {
                    //$whereclause .= " OR text_data LIKE '%".$value[$i]."%'"; error is here. 
                    $whereclause .= " OR text_data LIKE '%".$value."%'";
                    $i++;
                }
            $sql .= $whereclause; 
            $i++;     
        }
    
    PHP:
    Try this. Its working now.
     
    greatlogix, Nov 15, 2007 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    No it didn't work. It's starting with OR instead of AND. The first has to start with AND.
    I have:
    if (!empty($keyword1))
        {
            $myarray = explode(' ', $keyword1);
            foreach($myarray as $value)
                {
    	            $whereclause .= " AND text_data LIKE '%".$value."%'";
                }
            $sql .= $whereclause; 
    		$i++;       
        }
    if (!empty($keyword2))
    
        {
    
            $myarray = explode(' ', $keyword2);
    
    
    
            $whereclause .= " AND text_data LIKE '%".$value[0]."%'";
    
    
    
            foreach($myarray as $value)
    
                {
    
                    $whereclause .= " OR text_data LIKE '%{$value}%'";
    
                }
    
            $sql .= $whereclause;  
    
    
    
        }
    PHP:
    My query is supposed to start like this:
    But it's starting like this:
    So the search is displaying the entire book.
     
    gilgalbiblewheel, Nov 15, 2007 IP
  4. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #4
    After executing above code I got this

    In the line $whereclause .= " AND text_data LIKE '%".$value[0]."%'"; $value[0] is undefined so I am not getting value in sql statement
     
    greatlogix, Nov 15, 2007 IP
  5. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    I see. So how can I make it so that the AND will appear before?
     
    gilgalbiblewheel, Nov 15, 2007 IP
  6. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #6
    use this code


    $keyword2 = "how are you";
    if (!empty($keyword2))
    {
    $myarray = explode(' ', $keyword2);
    $i = 0;
    
    $i++;
    $a=0;
    foreach($myarray as $value)
    {
    	if($a==0){
    		$whereclause .= " AND text_data LIKE '%".$value[0]."%'";
    	}else{
    		$whereclause .= " OR text_data LIKE '%".$value."%'";
    	}
    	$a++;
    }
    $sql .= $whereclause;
    $i++;
    }
    echo $sql;
    
    PHP:
     
    salmanshafiq, Nov 15, 2007 IP
  7. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #7
    change one thing which is wrongly entered, try this one



    
    
    $keyword2 = "how are you";
    if (!empty($keyword2))
    {
    $myarray = explode(' ', $keyword2);
    $i = 0;
    
    $i++;
    $a=0;
    foreach($myarray as $value)
    {
        if($a==0){
            $whereclause .= " AND text_data LIKE '%".$value."%'";
        }else{
            $whereclause .= " OR text_data LIKE '%".$value."%'";
        }
        $a++;
    }
    $sql .= $whereclause;
    $i++;
    }
    echo $sql;
    
    
    PHP:
     
    salmanshafiq, Nov 15, 2007 IP
  8. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #8
    Excellent thanks a lot!
     
    gilgalbiblewheel, Nov 16, 2007 IP