Search with multiply words

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
GertK
Forum Newbie
Posts: 6
Joined: Fri Nov 01, 2013 8:45 am

Search with multiply words

Post by GertK »

I'm about to update my search script. The search engine searches in my database where I ask it to search. What is new is I want do some search words, that the users can check in a checkbox, if he wants to use the word in the search.

As for now my search engine works, the only problem is that it only searches the last word and not all of the checked words. My formula looks like this:

Code: Select all

<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>"> 
<p>Search for: 
</p> 
Books: <input type="checkbox" name='search' value="books"> 
Movies: <input type="checkbox" name='search' value="movies"> 
Outdoor: <input type="checkbox" name='search' value="outdoor"> 
Indore: <input type="checkbox" name='search' value="indore"> 
</p> 
<p><input type='submit'  value='Search'></p> 
</form> 
The php code looks like:

Code: Select all

<?php 
if(isset($_POST['search'])) 
{ 
  $connx = mysql_connect('localhost', '*******', ',*********') or die("connx"); 
  $db = mysql_select_db('*********') or die(mysql_error()); 
  
  # convert to upper case, trim it, and replace spaces with "|": 
  $search = mysql_real_escape_string($search); 
  $search = strtoupper(preg_replace('/\s+/', '|', ($_POST['search']))); 
  
  # create a MySQL REGEXP for the search: 
  $regexp = "REGEXP '[[:<:]]($search)[[:>:]]'"; 
  $query = "SELECT * FROM `keywords` WHERE UPPER(`keywords01`) $regexp OR ". 
           "`keywords02` $regexp OR ". 
           "`keywords03` $regexp OR ". 
           "`keywords04` $regexp"; 
  
  $result = mysql_query($query) or die($query . " - " . mysql_error()); 
  
echo "<table>\n"; 
while($row = mysql_fetch_assoc($result)) 

{ 
    echo "<tr>"; 
	echo "<td><img src=../{$row['type']}/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>";
    echo "<td>{$row['name']}</td>"; 
    echo "<td>{$row['date']}</td>"; 
    echo "<td><a href=../view.php?id={$row['id']} target=blank>VIEW</a></td>"; 
    echo "</tr>\n"; 
  }  
} 
 else {
  echo "<p>Sorry, no results matched your search.</p>";
}
?> 
Are there someone, who can figure out, why it is only the last marked checkboxs word that are searched and not all marked words and how do I get it to search for all marked words?

Hope someone can help.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Search with multiply words

Post by Christopher »

The data from the checkboxes in the form has multiple values. You need to separate them and add SQL to your WHERE clause for only the ones passed (those checked). If you print_r($search) you will see how the data is structured.
(#10850)
GertK
Forum Newbie
Posts: 6
Joined: Fri Nov 01, 2013 8:45 am

Re: Search with multiply words

Post by GertK »

Thanks for the reply. I have tried to play a little around with it. Im total new to PHP so its very difficult for me. I have changed my form to:
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">

Code: Select all

<p>Search for: 
</p> 
Books: <input type="checkbox" name='search[]' value="books"> 
Movies: <input type="checkbox" name='search[]' value="movies"> 
Outdoor: <input type="checkbox" name='search[]' value="outdoor"> 
Indore: <input type="checkbox" name='search[]' value="indore"> 
</p> 
<p><input type='submit'  value='Search'></p> 
</form> 
You write:
You need to separate them and add SQL to your WHERE clause for only the ones passed (those checked).
Im not sure how to do this part. Can you please help me - how should the codes look like?


When add:

Code: Select all

print_r($search)
and try search, it show the last checked box, but not the other checked boxes. This Again show that the script only search in the last checked box. All other boxes/values are overrulled by the last checked.

If I add the code:

Code: Select all

echo $query;
and check books and movies, I get the text:
[text]COUPLESELECT * FROM `keywords` WHERE UPPER(`keyword01`) REGEXP '[[:<:]](MOVIES)[[:>:]]' OR `keywords02` REGEXP '[[:<:]](MOVIES)[[:>:]]' OR `keywords03` REGEXP '[[:<:]](MOVIES)[[:>:]]' OR `keywords04` REGEXP '[[:<:]](MOVIES)[[:>:]]'[/text]

I need the script to search for all the checked the values and not only the last checked. Is that possible? Please advice. THANKS
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Search with multiply words

Post by Christopher »

Your preg_replace() is being passed an array (and you are escaping before $search is set). You need to loop through $search. To see what is passed, try:

Code: Select all

<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
<p>Search for:
</p>
Books: <input type="checkbox" name='search' value="books">
Movies: <input type="checkbox" name='search' value="movies">
Outdoor: <input type="checkbox" name='search' value="outdoor">
Indore: <input type="checkbox" name='search' value="indore">
</p>
<p><input type='submit'  value='Search'></p>
</form><?php
echo "<pre>" . print_r($_POST['search'], 1) . "</pre>";
?>
(#10850)
GertK
Forum Newbie
Posts: 6
Joined: Fri Nov 01, 2013 8:45 am

Re: Search with multiply words

Post by GertK »

Thanks for taking the time to answer me. I found out that if I replaced the lines:

----------------------------------------------------------------------------

$search = mysql_real_escape_string($search);
$search = strtoupper(preg_replace('/\s+/', '|', ($_POST['search'])));

----------------------------------------------------------------------------


With:

----------------------------------------------------------------------------

Code: Select all

$search = implode( '|', $_POST['search'] );
----------------------------------------------------------------------------

Then my script is working :)
Post Reply