Jump to content

Search / Table question


pure_skill_2000

Recommended Posts

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

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.