Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Search Script Bug.

    Hi all, I need some help/advice on a search script that I downloaded. It works perfectly when I search for a single word, but when I search for 2 words I get multiple responses. As I am new to php, and I did not write this script I am having trouble finding out why it is doing that and how to get around it. Any help would be greatly appreciated.

    To view the bug I have script here http://dev.loic.net.au/search.php and try searching for “item water” and then for just “item” or “water”

    PHP Code:
    <?php
    include ("includes/global.php");

    $template = new Template;
    $template->load("includes/default.htm"); 

    $connections mysql_connect($host$username$password) or die ( "Unabale to connect to the database" );

    mysql_select_db($db_name) or die ( "Unable to select database!" );

    $limit 10;

    $var = @$_GET['q'] ;
    $trimmed trim($var);
    $trimmed_array explode(" ",$trimmed);

    if (
    $trimmed == "") {
        
    $resultmsg =  "<p>Search Error</p><p>Please enter a search...</p>" ;
    }

    if (!isset(
    $var)){
        
    $resultmsg =  "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
    }

    foreach (
    $trimmed_array as $trimm) {

        
    $query "SELECT * FROM items WHERE title LIKE \"%$trimm%\" OR mini_dec LIKE  \"%$trimm%\" OR main_dec LIKE \"%$trimm%\" ORDER BY id" ;
        
        
    $numresults=mysql_query ($query);
        
    $row_num_links_main =mysql_num_rows ($numresults);
        
        if (empty(
    $s)) {
            
    $s=0;
        }
        
        
    $query .= " LIMIT $s,$limit" ;
        
    $numresults mysql_query ($query) or die ( "Couldn't execute query" );
        
    $rowmysql_fetch_array ($numresults);
        
        do{
            
    $adid_array[] = $row'id' ];
        }while( 
    $rowmysql_fetch_array($numresults));
    }

    if(
    $row_num_links_main == && $row_set_num == 0) {
        
    $resultmsg "<p>Search results for:" $trimmed  ."</p><p>Sorry, your search returned zero results</p>" ;
    }

    $tmparr array_unique($adid_array);
    $i=0;

    foreach (
    $tmparr as $v) {
        
    $newarr[$i] = $v
        
    $i++;
    }

    $body .= <<<EOD
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
            <form name="form1" method="get" action="search.php">
            <td>
            <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                <tr><td colspan="3"><strong>Search </strong></td></tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><input type="text" name="q" value="$var" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><input type="submit" value="Search"></td>
                </tr>
            </table>
            </td>
            </form>
        </tr>
    </table>
    <hr />
    EOD;

    if( isset (
    $resultmsg)) {
        
    $body .= $resultmsg;
        
    $template->replace("title""Search");
        
    $template->replace("meta""");
        
    $template->replace("body"$body);
        
    $template->publish();
        exit();
    } else {
        
    $body .=  "Search results for: " $var;
    }

    foreach(
    $newarr as $value) {

        
    $query_value "SELECT * FROM items WHERE id = '$value'";
        
    $num_value=mysql_query ($query_value);
        
    $row_linkcatmysql_fetch_array ($num_value);
        
    $row_num_linksmysql_num_rows ($num_value);
        
        
    $titlehigh preg_replace "'($var)'si" "<b>\\1</b>" $row_linkcat'title' ] );
          
    $linkhigh preg_replace "'($var)'si" "<b>\\1</b>" $row_linkcat'mini_dec' ] );
          
    $linkdesc preg_replace "'($var)'si" "<b>\\1</b>" $row_linkcat'main_dec' ] );

        foreach(
    $trimmed_array as $trimm) {
            if(
    $trimm != 'b' ){
                
    $titlehigh preg_replace"'($trimm)'si" ,  "<b>\\1</b>" $titlehigh);
                
    $linkhigh preg_replace"'($trimm)'si" "<b>\\1</b>" $linkhigh);
                
    $linkdesc preg_replace"'($trimm)'si" ,  "<b>\\1</b>" $linkdesc); 
             }
        
    $itemID $row_linkcat['id'];
    $body .= <<<EOD
    <p><a href="item.php?id=$itemID">$titlehigh</a><br>
    $linkhigh<br>
    $linkdesc</p>
    EOD;
        }
           if(
    $row_num_links_main $limit) {
            if (
    $s>=1) {
                
    $prevs=($s-$limit);
                 
    $body .=  "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit"</a></div>";
              }
         
         
    $slimit =$s+$limit;
             if (!(
    $slimit >= $row_num_links_main) && $row_num_links_main!=1) {
                 
    $n=$s+$limit;
                   
    $body .=  "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit"</a></div>";
            }
        }
    }  
      
    $template->replace("title""Search");
      
    $template->replace("meta""");
      
    $template->replace("body"$body);
      
    $template->publish();
    ?>

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,109
    Thanks
    2
    Thanked 326 Times in 318 Posts
    The code specifically searches for all rows with any of the entered words. It is doing an "item OR water" because of the multiple queries and the array_unique() function call.

    What do you want it to do?

    Edit: Before you just edited something on your site, the code was displaying both the title/short description and the long description, I would guess with different id's for each.

    I would recommend displaying what $adid_array and $newarr contain to make sure they are what you expect.
    Last edited by CFMaBiSmAd; 01-09-2008 at 10:40 PM. Reason: code on web site caused output to change, so first reply no longer holds
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Well I want it to not return multiple results. So if I do a search for “item water” it to return

    1) water
    2) item 1
    3) item 2
    4) item 3
    not
    1) water
    2) water
    3) item 1
    4) item 1
    5) item 2
    6) item 2
    7) item 3
    8) item 3

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,109
    Thanks
    2
    Thanked 326 Times in 318 Posts
    I found your logic error that is causing this (you need to use an editor that lets you match up { } - but closer examination of the indenting also shows it. A comment on closing }, indicating what they go with will help too, especially after an amount of time goes by or someone else needs to read your code) -

    PHP Code:
        foreach($trimmed_array as $trimm) { 
            if(
    $trimm != 'b' ){ 
                
    $titlehigh preg_replace"'($trimm)'si" ,  "<b>\\1</b>" $titlehigh); 
                
    $linkhigh preg_replace"'($trimm)'si" "<b>\\1</b>" $linkhigh); 
                
    $linkdesc preg_replace"'($trimm)'si" ,  "<b>\\1</b>" $linkdesc);  
             } 
        
    $itemID $row_linkcat['id']; 
    $body .= <<<EOD 
    <p><a href="item.php?id=$itemID">$titlehigh</a><br
    $linkhigh<br
    $linkdesc</p
    EOD
        } 
    In the code above, the last } shown is the end of the foreach() loop. This means that when $trimmed_array has two or more words, you get two or more identical lines concatenated onto $body.

    The last } shown in the above code should be moved up so it is right under the previous }.
    Last edited by CFMaBiSmAd; 01-09-2008 at 11:58 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Oh right, i see what you mean. Thanks heaps, thats all working now. that would of taken me ages to find

    by the way, what PHP IDE would you recommend? at the moment i am using Dreamweaver. which seems to be ok???


  •  

    Posting Permissions

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