Error in If statement

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

  1. #1
    I'm always getting an error:
        $Keyworda = $_GET["keyworda"]; 
    
    
        $sql = "Select * FROM bible WHERE 1=1";
    
    $i = 0
    
    if (!empty($Keyworda)) //line 31
        {
            $myarray = split($Keyworda, '+');
            foreach($myarray as $value)
                {
    	            $whereclause .= " AND text_data LIKE '%".$value."%'";
                }
            $sql .= $whereclause;        
        }
    PHP:
    It says:

     
    gilgalbiblewheel, Nov 6, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You forgot a semicolon here:
    
    $i = 0
    
    PHP:
    It should be:
    
    $i = 0;
    
    PHP:
     
    nico_swd, Nov 6, 2007 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    ok thanks.
    Ok it's fixed but how does the query pick up the keyword from the previous page:
            <form name="kjbook" action="search/cat/tableformat.php" method="get">
              <div id="div1"> <span style="position: absolute; left: 22px; top: 25px;"> Look for (a word or a set of words):<br />
                    <input class="Keywords"  id="Keyworda" name="Keyworda" title="The search will consist ALL of these words" />...
    </form>
    
    PHP:
    And on to the next page:
    $Keyworda = $_GET["keyworda"]; 
        $sql = "Select * FROM book WHERE 1=1";
    
    $i = 0;
    
    if (!empty($Keyworda))
        {
            $myarray = split('+', $Keyworda);
            foreach($myarray as $value)
                {
    	            $whereclause .= " AND text_data LIKE '%".$value."%'";
                }
            $sql .= $whereclause;        
        }
    PHP:
     
    gilgalbiblewheel, Nov 6, 2007 IP
  4. Demonic

    Demonic Active Member

    Messages:
    821
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #4
    What exactly are you trying to do give more detail and it would be easier for us to help you out.
     
    Demonic, Nov 6, 2007 IP
  5. live-cms_com

    live-cms_com Notable Member

    Messages:
    3,128
    Likes Received:
    112
    Best Answers:
    0
    Trophy Points:
    205
    Digital Goods:
    1
    #5
    live-cms_com, Nov 6, 2007 IP
  6. hiredgunz

    hiredgunz Peon

    Messages:
    203
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    the method="get" part of the form tag. You should probably change it to use the POST method instead.

    Let say you have a url that has this in it

    
    http://www.yourdomain.com?key=value
    
    Code (markup):
    When you use GET, it will "get" the value of "key" which is "value" ...

    
    $myvalue = $_GET['key'];
    /// $myvalue now equals 'value'
    
    Code (markup):
    To change it to the post method, change the method="get" to method="POST", and then on the second page, change the $Keyworda = $_GET["keyworda"]; to $Keyworda = $_POST["keyworda"];

    Hope that helps,

    Jan
     
    hiredgunz, Nov 6, 2007 IP
  7. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #7
    Ok so PHP is case sensitive. It leads to the following:
        $keyworda = $_REQUEST["keyworda"];
        $keywordb = $_REQUEST["keywordb"];
        $keywordc = $_REQUEST["keywordc"];
        $keywordd = $_REQUEST["keywordd"];
        $keyworde = $_REQUEST["keyworde"];
        $keywordf = $_REQUEST["keywordf"];
        $keywordg = $_REQUEST["keywordg"];
        $keywordh = $_REQUEST["keywordh"];
        $spoke = $_GET["spoke"]; //Request.Querystring("spoke")
        $number = $_GET["number"]; //Request.QueryString("number")
        $id = $_GET["id"]; //Request.QueryString("id")
    
        $sql = "Select * FROM book WHERE 1=1";
    
    $i = 0;
    
    if /*($keyworda != "")*/(!empty($keyworda))
        {
            $myarray = split('+', $keyworda); //line 33
            foreach($myarray as $value)//line 34
                {
    	            $whereclause .= " AND text_data LIKE '%".$value."%'";
                }
            $sql .= $whereclause;        
        }
    if ($keywordb != "")
        {
            $myarray = split('+', $keywordb);//line 43
    
            foreach($myarray as $value)//line 44
                {
                	$whereclause .= " AND text_data LIKE '%".$value."%'";
    				$i++;
                }
            $sql .= $whereclause;        
        }
    if ($keywordc != "")
        {
            $myarray = split('+', $keywordc);//line 53
    
            foreach($myarray as $value)//line 53
                {
                	$whereclause .= " AND text_data LIKE '%".$value."%'";
                }
            $sql .= $whereclause;        
        }
            if ($i != 0) 
    			{
    				$sql.= " AND ";
            $i ++;
    			}
            $sql.= " AND text_data LIKE '%".$keywordc."%' ";
    PHP:
     
    gilgalbiblewheel, Nov 6, 2007 IP
  8. Demonic

    Demonic Active Member

    Messages:
    821
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #8
    Remove the color tags from your PHP code so I can read through it.

    also try explode() instead of split.
     
    Demonic, Nov 6, 2007 IP
  9. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #9
    Explode()?
     
    gilgalbiblewheel, Nov 6, 2007 IP
  10. hiredgunz

    hiredgunz Peon

    Messages:
    203
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It takes a string and "explodes" it using a predetermined seperator...

    
    $my_string = "This is a string";
    
    $new_string = explode(" ",$my_string); 
    // The empty aspostophes are saying to break the string at all whitespaces ... I could have put a "i" there instead and the string would have been broken up at each letter i in the string
    
    // $new_string[0] = This
    //$new_string[1] = is
    //$new_string[2] = a
    //$new_string[3] =string
    
    Code (markup):
    Hope this helps,

    jan
     
    hiredgunz, Nov 6, 2007 IP
  11. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #11
    So how would this break apart?
    if (!empty($keyworda))/*($keyworda != "")*/
        {
            $myarray = explode('+', $keyworda);
            foreach($myarray as $value)
                {
    	            $whereclause .= " AND text_data LIKE '%".$value."%'";
                }
            $sql .= $whereclause;        
        }
    PHP:
    Where foreach has to make a loop until the exploded words are finished?
     
    gilgalbiblewheel, Nov 6, 2007 IP
  12. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #12
    ok I removed the "+" and inserted " ".
     
    gilgalbiblewheel, Nov 6, 2007 IP
  13. hiredgunz

    hiredgunz Peon

    Messages:
    203
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #13
    when you explode a string, it becomes an array of values .. what your foreach is doing is taking each value to make an sql lookup for that value...

    You might try using isset in your if (!empty($keyworda))/ like this

    if (isset($keyworda))

    best,

    jan
     
    hiredgunz, Nov 6, 2007 IP
  14. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #14
    No how about making the first of the array "AND..." and the following "OR..."

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

    hiredgunz Peon

    Messages:
    203
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #15
    thats probably not going to work for you :)

    Your foreach is already looping thru the array and theres no need to loop thru the exploded array above the foreach.... you don't need the itineration i++

    Plus, you can have and/or in your sql query so no need to split that up into two loops....

    if (isset($keywordb))
    {
    $myarray = explode(' ', $keywordb);
    foreach($myarray as $value)
    {
    $whereclause .= " AND text_data LIKE '%".$value."%' OR text_data
    LIKE '%".$value."%'";
    }
    $sql .= $whereclause;
    }

    This is just off the top of my head so forgive me if I missed a little charactor or something (I'm old lol)

    best,

    Jan
     
    hiredgunz, Nov 6, 2007 IP
  16. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #16
    Yeah but let's say I have 4 words that need to be exploded then the query would be like this:
    $whereclause .= " AND text_data LIKE '%".$value."%' OR text_data
    LIKE '%".$value."%'" AND text_data LIKE '%".$value."%' OR text_data
    LIKE '%".$value."%'";
     
    gilgalbiblewheel, Nov 6, 2007 IP
  17. hiredgunz

    hiredgunz Peon

    Messages:
    203
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #17
    theres probably some slick complicated way to do that, but if you aren't doing major queries on huge databases, then theres nothing that says you can't use multiple comparisons in one line of a mysql query.....

    best,

    jan
     
    hiredgunz, Nov 7, 2007 IP