If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > Problems with simple search engines

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-04-10, 12:26
supadema supadema is offline
Registered User
 
Join Date: Jul 2010
Posts: 18
Problems with simple search engines

Hello everybody,

I'm trying to do a simple internal search engines for my website. What I was able to do till now is a little too much simple and also doesn't work very good...

Clicking on the button research you are addressed on search.php (of which I post the code below).
The research is necessary to find a kind of schedules composed by an image, a title and a description.
I would like that writing one or more words in the research form, I can get the results where appear those words, if is possible in a rilevance order.

Now, the problems are:

- if I write for example 2 words, separated by a space, and both the 2 words are in the same record, that record will be written 2 times.
- I can't limit the number of results using LIMIT... it doesn't work, I get all the results where that word exist.
- I could go on with the problems but these are already enough....

If somebody had a little of time to help me or at least suggest me a better way, I would be really grateful!!!

PHP Code:
if ($_POST['search']){
if (!empty(
$_POST['keywords'])) {
$searchStr $_POST['keywords'];
if (
ereg("^[0-9a-zA-Zà-ù,&,'.' ]+$"$searchStr)) {

require (
"db/db.php");

print 
"<div class='risultati'>Risultati della ricerca per \"<b>$searchStr</b>\":</div><br>";
$keys explode(" "$searchStr);

for (
$x 0$x count($keys); $x++) {
$querystr "SELECT img,link,numclick,descrizione,ordine FROM totale_tabelle WHERE descrizione = \"$keys[$x]\" OR descrizione LIKE \"%$keys[$x]%\" OR ordine = \"$keys[$x]\" OR ordine LIKE \"%$keys[$x]%\"";

$result mysql_query($querystr);
if (
$frow mysql_fetch_array($result)) {
$found true;
do { 

$words $frow["descrizione"];
$boldwords str_replace($searchStr,"<b>".$searchStr."</b>",$words);

echo 
"<div class='grid_8'><div class='screen'>".$frow["img"]."</div><div class='text'><h2>".$frow["link"]."<span class='visite'> &nbsp;- ".$frow["numclick"]." visite</span></h2>".$boldwords."</div></div>"."<br>";
} while (
$frow mysql_fetch_array($result));
}

}
if (!
$found) {
print(
"<div class='risultati'><b>Nessun risultato correlato alla ricerca è stato trovato.</b></div><br>");
}

}
else {
print(
"<div class='risultati'><br><b>La tua stringa di ricerca contiene caratteri non ammessi.</b></div><br>");
}
}
else {
print(
"<div class='risultati'><br><b>Devi inserire almeno una parola chiave per effettuare la ricerca.</b></div><br>");
}

Reply With Quote
  #2 (permalink)  
Old 08-04-10, 13:31
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Quote:
Originally Posted by supadema
If somebody had a little of time to help me or at least suggest me a better way, I would be really grateful!!!
I would just build the query string like this and just run the query once ie:
Code:
$keys = explode(" ", $searchStr);

$querystr = "SELECT img,link,numclick,descrizione,ordine FROM totale_tabelle WHERE 1=1 ";

for ($x = 0; $x < count($keys); $x++) {
   $querystr .= "and concat( descrizione,' ', ordine ) like \"%$keys[$x]%\"";
}

$querystr .= " limit 20";

$result = mysql_query( $querystr );
This should be faster, produce the matches you want and have a limit on the number of records returned. I haven't tested it so there may be syntax errors in the code but hopefully you get the idea.
__________________
Mike

Last edited by mike_bike_kite; 08-04-10 at 15:39.
Reply With Quote
  #3 (permalink)  
Old 08-04-10, 15:10
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 8,768
I'd also suggest you filter out user input to remove duplicate words
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 08-05-10, 11:38
supadema supadema is offline
Registered User
 
Join Date: Jul 2010
Posts: 18
Quote:
This should be faster, produce the matches you want and have a limit on the number of records returned. I haven't tested it so there may be syntax errors in the code but hopefully you get the idea.
Thanks!! Like this is much better!
I've no more duplicated results and now I can limit the number of results as I want.

I have just some problems now with the last part of the code:

PHP Code:
if (!$found) {
print(
"<div class='risultati'><b>No results found</b></div><br>");
}

}
else {
print(
"<div class='risultati'><br><b>Your keywords contain characters not allowed</b></div><br>");
}
}
else {
print(
"<div class='risultati'><br><b>You must insert at least one keyword</b></div><br>");

If I use characters not allowed I get the written "No results found", if I don't write anything I get "Your keywords contain characters not allowed".
And also I'm still not allowed to write à-è-ì-ò-ù using this in my code if (ereg("^[0-9a-zA-Zà-ù,&amp;,'.' ]+$", $searchStr))... don't know what's wrong here...
Reply With Quote
  #5 (permalink)  
Old 08-05-10, 13:32
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 8,768
ca\n we see the if statement that precedes the if (!$found)?
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 08-05-10, 14:07
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
A few comments:
  • You separate the search words with spaces but then raise an error as spaces are not allowed in your input string test!
  • Why stop the search if nothing is entered? - just run the query and limit the results as normal.
  • Why not just quietly strip bad characters from the search string - this saves another test.
  • Why do you have <div> tokens in your html?
  • You should get everything working without the accented characters first.
  • You could also try indenting your code to make it easier to read.
  • You could also experiment with comments.
__________________
Mike
Reply With Quote
  #7 (permalink)  
Old 08-05-10, 15:20
supadema supadema is offline
Registered User
 
Join Date: Jul 2010
Posts: 18
Quote:
* You separate the search words with spaces but then raise an error as spaces are not allowed in your input string test!

* Why not just quietly strip bad characters from the search string - this saves another test.
I'm sorry, I've not understood exactly what you mean....

Quote:
* Why stop the search if nothing is entered? - just run the query and limit the results as normal.
ok, understood

Quote:
* Why do you have <div> tokens in your html?
because I need to give some styles from css to the text I get from mysql...

Quote:
* You should get everything working without the accented characters first.
yes i know, it was just one of the problems

Quote:
* You could also try indenting your code to make it easier to read.
* You could also experiment with comments.
you're right.
I'm still a beginner with php, and the code i've put together I've taken from sources I found on the web, but without understanding it in all the steps...
Reply With Quote
  #8 (permalink)  
Old 08-05-10, 16:19
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
* You separate the search words with spaces but then raise an error as spaces are not allowed in your input string test!
I made a mistake here - I can now see a space so everything is OK

* Why not just quietly strip bad characters from the search string - this saves another test.
Use a function like ereg_replace to simply remove invalid charcters from the search string.

I'm still a beginner with php, and the code i've put together I've taken from sources I found on the web, but without understanding it in all the steps...
You'll learn slowly this way and never have confidence in your code. Better to write the comments first. Then double check these comments to make sure the program will do what you want it to do. Finally work out how to write the code that goes with each comment. Add the fancy bits later like accented characters.
__________________
Mike
Reply With Quote
  #9 (permalink)  
Old 08-16-10, 10:42
tsquez tsquez is offline
Registered User
 
Join Date: Aug 2010
Location: Rimrock, AZ
Posts: 1
Ok I was going to post here:

search keywords on multiple tables

but was redirected to this post, and this post clarified more stuff for me. Thanks mike_bike its greatly appreciated.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On