Jump to content

Search with multiply words


GertK

Recommended Posts

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:

<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:

<?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.

Link to comment
Share on other sites

Hi, Tried that:

<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> 

But nothing happens! I think that there also needs to be done some recoding in the script, right?

Link to comment
Share on other sites

 


I think that there also needs to be done some recoding in the script, right?

 

Of course :-) You cannot just replace a string with an array and expect it to work.

 

You will have to loop through the array and add each word to the query.

 

And you newed to do something about your datamodel, right now you have keyword1, keyword2, keyword3, that's very inefficient and slow to work with, it's better to create a separate table that holds one record per keyword. That will also make this query a lot easier to do because you only have to search one column, instead of three or four (the keyword can occur in all colums so you have to search for each keyword three or four times... icky!)

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.