Search Engine Multiple word search problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mashamit
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2011 4:41 pm

Search Engine Multiple word search problem

Post by mashamit »

Good Evening everyone,

Im in need of a little help, I have a search engine script that queries a mysql database which I have found and adjusted for my usage, it works very well except for the following issue.

You can search for single words or part words
You can search for a multiple words or a phrase and it will output results if the exact string is found.

I cant seem to get it to pick all results that contain any combination of the search words

for instance

cake - ok
fruit cake - ok
fruit - ok
cake fruit - not found

ill post the code below, if anyone can suggest a change in code, that would be great.

Code: Select all

<?php


//get data
$button = $_GET['submit'];
$search = $_GET['search'];


$s = $_GET['s'];
if (!$s)
$s = 0;


$e = 50; // Just change to how many results you want per page


$next = $s + $e;
$prev = $s - $e;




 if (strlen($search)<=0)
   echo "<table width='500px'>
<td WIDTH='500px'>You must enter text into the search <a href='index1.php'>Click here to try again</a></td>

</tr>
</table>";
  
 else
 {
  echo "<br /><table><tr><td></td><td><form action='search1.php' method='GET'><input type='text' onclick=value='' size='50' name='search' value='$search'> <input type='submit' name='submit' value='Search'></form></td></tr></table>";
  
  //connect to database
  mysql_connect("localhost","****","*****");
  mysql_select_db("***");
   
   //explode out search term
   $search_exploded = explode("*",$search);
   
   foreach($search_exploded as $search_each)
   {
   
        //construct query
    $x++;
    if ($x==1)
     $construct .= "Title LIKE '%$search_each%' OR Contributor LIKE '%$search_each%' OR Course LIKE '%$search_each%' OR Client LIKE '%$search_each%' OR Year LIKE '%$search_each%' OR Series LIKE '%$search_each%' OR Week LIKE '%$search_each%' OR Code LIKE '%$search_each%' OR Day LIKE '%$search_each%' OR Foodcourse LIKE '%$search_each%' OR Prog LIKE '%$search_each%' OR Foodcategory LIKE '%$search_each%'OR Book LIKE '%$search_each%' ";
   
   }
   
  //echo outconstruct
  $constructx = "SELECT * FROM Recipes WHERE $construct";
  
  $construct = "SELECT * FROM Recipes WHERE $construct LIMIT $s,$e";
  $run = mysql_query($constructx);
  
  $foundnum = mysql_num_rows($run);


  $run_two = mysql_query("$construct");
  
  if ($foundnum==0)
   echo "No results found for <b>$search</b>";
  else
  {
   echo "<table bgcolor='#0000FF' width='100%' height='1px'><br /></table><table bgcolor='#f0f7f9' width='100%' height='10px'><tr><td><div align='right'><b>$foundnum</b> results found for <b>$search.</b></div></td></tr></table><p>";
   
   while ($runrows = mysql_fetch_assoc($run_two))
   {
    //get data
   $title = $runrows['Title'];
   $fcourse = $runrows['Foodcourse'];
   $url = $runrows['Links'];
   $image = $runrows['image'];
   
   echo "<table width='800px' border='1'>
<td width='300px'><b>$title</b></td>
<td WIDTH='150px' div align='center'>$fcourse</td>
<td WIDTH='150px' div align='center'><a href='recipes/$url'>Click here for Recipe</a></td>
<td WIDTH='200px' height='100px' div align='center'><img src='images/$image'></td>

</tr>
</table>";
   }
?>
	

<table width='100%'>
<p>&nbsp;</p>
<tr>
<td>
<div align="center">

<?php
if (!$s<=0)
 echo "<a href='search1.php?search=$search&s=$prev'>Prev</a>";

$i =1; 
for ($x=0;$x<$foundnum;$x=$x+$e)
{


 echo " <a href='search1.php?search=$search&s=$x'>$i</a> ";


$i++;


}

if ($s<$foundnum-$e)
  echo "<a href='search1.php?search=$search&s=$next'>Next</a>";

	}
}  


?>

thank you in advance :)
Dorin85
Forum Newbie
Posts: 20
Joined: Tue Aug 16, 2011 3:16 pm

Re: Search Engine Multiple word search problem

Post by Dorin85 »

//explode out search term
$search_exploded = explode("*",$search);
Shouldn't you be exploding on " " rather than "*"?
mashamit
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2011 4:41 pm

Re: Search Engine Multiple word search problem

Post by mashamit »

Oh dear, I feel a little stupid now :) that works perfectly, not sure how I ended up with that in there. Many thanks
Post Reply