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:
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:
"Ok it's fixed but how does the query pick up the keyword from the previous page:" www.domain.com/search/cat/tableformat.php?Keyworda=something $Keyworda = $_GET["keyworda"]; There could be a possible problem with case sensitivity though, your form says Keyworda but your $_GET says keyworda.
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
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:
Remove the color tags from your PHP code so I can read through it. also try explode() instead of split.
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
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?
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
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:
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
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."%'";
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