Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2010
    Posts
    20

    Unanswered: 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>");
    }


  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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.
    Last edited by mike_bike_kite; 08-04-10 at 15:39.

  3. #3
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I'd also suggest you filter out user input to remove duplicate words
    I'd rather be riding on the Tiger 800 or the Norton

  4. #4
    Join Date
    Jul 2010
    Posts
    20
    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...

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    ca\n we see the if statement that precedes the if (!$found)?
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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.

  7. #7
    Join Date
    Jul 2010
    Posts
    20
    * 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....

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

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

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

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

  8. #8
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    * 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.

  9. #9
    Join Date
    Aug 2010
    Location
    Rimrock, AZ
    Posts
    1
    Ok I was going to post here:

    http://www.dbforums.com/mysql/165890...le-tables.html

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •