Jump to content

Recommended Posts

I have a web site with several thousand items, all the items are stored in a mysql database. I am trying to have it to where the user can enter a phrase and the search would pull the rows that relate.

 

Problem: The way that I have it written now, users can only enter searches containing one or two words. And it works great with one or two words. But, I would like to update it to where if the client enters Ex. "Sx-495 Belt Starter" that it will still pull rows from the database with those words.

 

Here's the way it is set up now. And they way I have these "or's" in the select statement, is this meaning one row or the other?

function get_search_books($mysearch)
{
   // query database for the books in a category
  $mysearch =$_POST['searchlike'];
  $mysearch= trim($mysearch);
   if (!$mysearch || $mysearch=='')
     return false;
   
   $conn = db_connect();
   

$sql = "SELECT No, description, title, author, isbn, price FROM books ";
$sql .= "WHERE `No` LIKE '%$mysearch%' ";
$sql .= "OR `description` LIKE '%$mysearch%' ";
$sql .= "OR `title` LIKE '%$mysearch%' ";
$sql .= "OR `author` LIKE '%$mysearch%' ";
$sql .= "OR `isbn` LIKE '%$mysearch%' "; 

   $result = @$conn->query($sql);
   if (!$result)
     return false;
   $num_books = @$result->num_rows;
   if ($num_books ==0)
      return false;
   $result = db_result_to_array($result);
   return $result;
}

 

Thanks,

Jake

Link to comment
https://forums.phpfreaks.com/topic/72608-solved-searching-phrases/
Share on other sites

this SHOULD work for multiple words , althogh it would have to consists of all the words mentinoed for it to work, how about your split the search terms by spaces like if they entere three words "this my search" then you would have $s[0]='this' $s[1]='my' $s[2]='search' use split(" ",$mysearch) for this.

 

Now that you have the search terms split, loop throgh them:

 

foreach($s as $mysearch) {

 

}

 

FULL CODE BELOW:

 

function get_search_books($mysearch)
{
   // query database for the books in a category
  $mysearch =$_POST['searchlike'];
  $mysearch= trim($mysearch);
   if (!$mysearch || $mysearch=='')
     return false;
   
   $conn = db_connect();

/* START OF EDIT */

$MAXWORDS = 5; /* It has to query PER word, so set the max search terms here.  It will treat the last search term as all remaining words that have not been searched for, so if this is 3 then the third search to the db is everything NOT in the second and first searches. */
   
$s = split(' ', $mysearch, $MAXWORDS);
$result = ''; /* This sets result as a blank string , to aviod the NOTICE of appening data to nothing */

foreach($s as $mysearch) {

  $sql = "SELECT No, description, title, author, isbn, price FROM books ";
  $sql .= "WHERE `No` LIKE '%$mysearch%' ";
  $sql .= "OR `description` LIKE '%$mysearch%' ";
  $sql .= "OR `title` LIKE '%$mysearch%' ";
  $sql .= "OR `author` LIKE '%$mysearch%' ";
  $sql .= "OR `isbn` LIKE '%$mysearch%' "; 

  $result.= @$conn->query($sql); /* Notice the . before the = , this appends the results to the previous results. */

} /* END OF EDIT */

   if (!$result)
     return false;
   $num_books = @$result->num_rows;
   if ($num_books ==0)
      return false;
   $result = db_result_to_array($result);
   return $result;
}

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.