pure_skill_2000 Posted April 20, 2008 Share Posted April 20, 2008 Hi for some reason the following code works but with one glich, it doesnt return only the records with the search criteria, its returns the whole table - anyone with any ideas? Thanks! //search variable = data in search box or url if(isset($_GET['search'])) { $search = $_GET['search']; } //trim whitespace from variable $search = trim($search); $search = preg_replace('/\s+/', ' ', $search); //seperate multiple keywords into array space delimited $keywords = explode(" ", $search); //Clean empty arrays so they don't get every row as result $keywords = array_diff($keywords, array("")); //Set the MySQL query if ($search == NULL or $search == '%'){ } else { for ($i=0; $i<count($keywords); $i++) { $query = "SELECT * FROM LFA " ; "WHERE column1 LIKE ‘%".$keywords[$i]."%’"; " ORDER BY column1"; } //Store the results in a variable or die if query fails $result = mysql_query($query) or die(mysql_error()); } if ($search == NULL or $search == '%'){ } else { //Count the rows retrived $count = mysql_num_rows($result); } echo "<html>"; echo "<head>"; echo "<title>Low flying areas</title>"; echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />"; echo "</head>"; echo "<body onLoad=\"self.focus();document.searchform.search.focus()\">"; echo "<center>"; echo "<br /><form name=\"searchform\" method=\"GET\" action=\"findlfa.php\">"; echo "<input type=\"text\" name=\"search\" size=\"20\" TABINDEX=\"1\" />"; echo " <input type=\"submit\" value=\"Search\" />"; echo "</form>"; //If search variable is null do, else print it. if ($search == NULL) { } else { echo "You searched for <b><FONT COLOR=\"blue\">"; foreach($keywords as $value) { print "$value "; } echo "</font></b>"; } echo "<p> </p><br />"; echo "</center>"; //If users doesn't enter anything into search box tell them to. if ($search == NULL){ echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>"; //ELSE print the data in a table } else { //Table header echo "<center><table id=\"search\" bgcolor=\"#AAAAAA\">"; echo "<tr>"; echo "<td><b>COLUMN 1:</b></td>"; echo "<td><b>COLUMN 2:</b></td>"; echo "<td><b>COLUMN 3:</b></td>"; echo "<td><b>COLUMN 4:</b></td>"; echo "<td><b>COLUMN 5:</b></td>"; echo "<td><b>COLUMN 6:</b></td>"; echo "<tr>"; echo "</table></center>"; //Colors for alternation of row color on results table $color1 = "#d5d5d5"; $color2 = "#e5e5e5"; //While there are rows, print it. while($row = mysql_fetch_array($result)) { //Row color alternates for each row $row_color = ($row_count % 2) ? $color1 : $color2; //table background color = row_color variable echo "<center><table bgcolor=".$row_color.">"; echo "<tr>"; echo "<td>".$row['LFA']."</td>"; echo "<td>".$row['LFA Brief']."</td>"; echo "<td>".$row['Link']."</td>"; echo "</tr>"; echo "</table></center>"; $row_count++; //end while } //end if } echo "</body>"; echo "</html>"; if ($search == NULL or $search == '%') { } else { //clear memory mysql_free_result($result); } ?> Link to comment https://forums.phpfreaks.com/topic/102051-search-table-question/ Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 You are not contencating your strings correctly. use the dot ('.') operator. Also you are over writing the $query variable int the loop This might work after a little debuging <?php //Set the MySQL query if ($search == NULL or $search == '%'){ } else { $conditions = array(); for ($i=0; $i<count($keywords); $i++) { $condtions .= "column1 LIKE ‘%" . $keywords[$i] . "%’ " . } $query = "SELECT * FROM LFA " . "WHERE column1 LIKE " . implode(' OR ' . $conditions). " ORDER BY column1"; ?> Also the fact that you missed this means, that you are not paying attention to your error messages. Make sure that you have error_reporting set to E_ALL and that you read and correct each error. Link to comment https://forums.phpfreaks.com/topic/102051-search-table-question/#findComment-522291 Share on other sites More sharing options...
pure_skill_2000 Posted April 20, 2008 Author Share Posted April 20, 2008 This seems to have no lost everything my search box isnt displayed Link to comment https://forums.phpfreaks.com/topic/102051-search-table-question/#findComment-522306 Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 No, I'm not debugging/rewriting your code for you. I'm just providing an example of how you can handle that one situation. It is your responsibilty to change your own code. If you want to integrate my example into your code, thats fine with me. Link to comment https://forums.phpfreaks.com/topic/102051-search-table-question/#findComment-522309 Share on other sites More sharing options...
pure_skill_2000 Posted April 20, 2008 Author Share Posted April 20, 2008 Sorry really didnt mean to cause any offence and tbh honestly didnt know where the search etc had gone and how I had manged 2 lose it, sorry again! I didnt mean it like that at all, it was a geniune honest question Link to comment https://forums.phpfreaks.com/topic/102051-search-table-question/#findComment-522317 Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 It's probably my fault. So many ask others to write code for them that I just assume that is what people are wanting. Anyways, if you can integrate my example into your code and fix a few syntax errors it, it should work. Link to comment https://forums.phpfreaks.com/topic/102051-search-table-question/#findComment-522318 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.