Jump to content

search help


shcKr-

Recommended Posts

i have a problem, i used a UNION so it would search all the tables and now it is picking up all of the items there and not filtering them by what i searched :S

 

this is what it is doing : http://bluid.co.uk/new/search.php?search=bed

 

and heres my code:

 

<?php
require 'config.php';
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
if(isset($_GET['search']))
{
$search = $_GET['search'];
}
$search = trim($search);
$search = preg_replace('/\s+/', ' ', $search);
$keywords = explode(" ", $search);
$keywords = array_diff($keywords, array(""));
if ($search == NULL or $search == '%'){
} else {
for ($i=0; $i<count($keywords); $i++) {
$query = "SELECT * FROM accessories UNION SELECT * FROM bedroom UNION SELECT * FROM chaises UNION SELECT * FROM contemporary UNION SELECT * FROM dining UNION SELECT * FROM garden UNION SELECT * FROM kids UNION SELECT * FROM lighting UNION SELECT * FROM living UNION SELECT * FROM mirrors " .
"WHERE title LIKE '%".$keywords[$i]."%'".
" ORDER BY title";
}
$result = mysql_query($query) or die(mysql_error());
}
if ($search == NULL or $search == '%'){
} else {
$count = mysql_num_rows($result);
}

echo "<html>";
echo "<head>";
echo "<title>Your Title Here</title>";
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />";
echo "</head>";
echo "<body onLoad=\"self.focus();document.searchform.search.focus()\">";
echo "<h2>Search</h2>";
echo "";
echo "<form name=\"searchform\" method=\"GET\" action=\"index.php?search\">";
echo "<input type=\"text\" name=\"search\" size=\"20\" TABINDEX=\"1\" />";
echo " <input type=\"submit\" value=\"Search\" style=\"border:1px solid #f2f2f2;background-color:#d7d7d7;font-weight:bold;font-family:Tahoma;color:#888888;height:22px;\"/>";
echo "</form><br>";
if ($search == NULL) {
} else {
echo "<span style=\"text-transform:capitalize\">You searched for "<b><FONT COLOR=\"#198bc7\">";
foreach($keywords as $value) {
   print "$value";
}
echo "</font></span></b>"";
}
echo "";
echo "";
if ($search == NULL){
echo "<b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br />";
} elseif ($search == '%'){
echo "<b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br />";
} elseif ($count <= 0){
echo "<b><FONT COLOR=\"red\">Your query returned no results from the database.</font></b><br />";
} else {
echo "<center><br><table id=\"search\" bgcolor=\"#\" width=\"100%\">";
echo "<tr>";
echo "<td height=\"30\"><b>SEARCH RESULTS: </b></td>";
echo "</tr>";
$color1 = "#efefef";
$color2 = "#";
$color3 = "#177cb1";
$color4 = "#299cd8";
while($row = mysql_fetch_array($result))
{
$row_color = ($row_count % 2) ? $color1 : $color2;
$text_color = ($row_count % 2) ? $color4 : $color3;
echo "<tr bgcolor=".$row_color." style=\"color:#198bc7;padding:10px\">";
echo "<td style=\"padding:5px;padding-left:20px;color:".$text_color."\"><strong>- ".$row['title']."</strong></td>";
echo "</tr>";
$row_count++;
}
echo "</table>";
}
echo "</body>";
echo "</html>";
if ($search == NULL or $search == '%') {
} else {
mysql_free_result($result);
}
?> 

Link to comment
Share on other sites

this is my explain, i think this is what you wanted, all my tables are like this:

 

Field 	Type 	Null 	Key 	Default 	Extra
id 	int(5) 	NO 	PRI 	NULL 	auto_increment
title 	varchar(255) 	NO 	  	NULL 	 
productid 	varchar(99) 	NO 	  	NULL 	 
type 	varchar(100) 	NO 	  	NULL 	 
range 	varchar(255) 	NO 	  	NULL 	 
price 	varchar(50) 	NO 	  	NULL 	 
size1 	varchar(50) 	NO 	  	NULL 	 
size2 	varchar(50) 	NO 	  	NULL 	 
size3 	varchar(50) 	NO 	  	NULL 	 
size4 	varchar(50) 	NO 	  	NULL 	 
size5 	varchar(50) 	NO 	  	NULL 	 
filename 	varchar(50) 	NO 	  	NULL 	 
filename2 	varchar(50) 	NO 	  	NULL 	 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.