SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search Integration

    Hi all,

    I want to integrate a search tool into my site.
    It is based on one page using get to send variables to itself.

    How do I change it so that it uses two pages. One to act as a form to take in the variables. The other to search.

    Here is my search form:
    PHP Code:
     <form action="http://mysite/search/search.php" method="post">
     <
    label for="search" class="bold">Search:</label>
     <
    input type="text" name="query" id="search" />
     <
    br />
     <
    input type="submit" name="submit" value="Submit" />
     <
    br /><br />
     </
    form
    Here is the code I need to integrate with the above form(works fine if used without the above form)...
    PHP Code:
     <?
     
    /*******************************************
     * Sphider Version 1.2.x
     * This program is licensed under the GNU GPL.
     * By Ando Saabas          ando(a t)cs.ioc.ee
     ********************************************/
     
    error_reporting(E_ALL E_NOTICE E_WARNING); 
     
     
     
    $include_dir "./include"
     
    $language_dir "./languages";
     
     include 
    "$include_dir/connect.php";
     include 
    'searchfuncs.php';
     
     
    extract(getHttpVars());
     
     include 
    "$include_dir/index_header.inc";
     include 
    "$include_dir/conf.php";
     
     
     
     if (
    $results != "") {
         
    $results_per_page $results;
     }
     
     if (
    get_magic_quotes_gpc()==1) {
         
    $query stripslashes($query);
     } 
     if (
    $type == "phrase") {
         
    $check3 "checked";
     } else if (
    $type == "or") {
         
    $check2 "checked";
     } else {
         
    $check1 "checked";
     }
     
     
    ?>
     <center><table cellpadding="5" 
     cellspacing="1" class="searchBox"><tr><td align="center">
     
     <form action="<?print $_SERVER['PHP_SELF'];?>" method="get">
     <input type="text" name="query" size="40" value="<?  print quote_replace($query);?>">
     <input type="submit" value="<?print $sph_messages['Search'];?>">   
     <?
     
         
    if ($catid<>0) {
             
    $query "SELECT category FROM ".$mysql_table_prefix."categories WHERE category_id='$catid'";
             
    $result mysql_query($query);
             echo 
    mysql_error();
             
    $row mysql_fetch_array($result);
             
    $category $row['category'];
     
             print 
    "<center><b>".$sph_messages['Search']."</b>: <input type=\"radio\" name=\"category\" value=\"$catid\">".$sph_messages['Only in category']." \"$category\"  <input type=\"radio\" name=\"category\" value=\"-1\" checked>".$sph_messages['All sites']."</center>";
         }
     
    ?>
     
     <input type="hidden" name="search" value="1">
     
     </form>
     <?
         
         $get_cats 
    "SELECT * FROM ".$mysql_table_prefix."categories WHERE parent_num=0 ORDER BY category";
         
    $result mysql_query($get_cats);
         echo 
    mysql_error();
         
    $rows mysql_num_rows($result);
     
         if (
    $rows >&& $search==1
             print 
    "<a href=\"".$_SERVER['PHP_SELF']."\">".$sph_messages['Categories']."</a>";
     
    ?>
     </td></tr></table>
     </center>
     
     <?
     
     
     
    function getmicrotime(){
         list(
    $usec$sec) = explode(" ",microtime());
         return ((float)
    $usec + (float)$sec);
         }
     
     
     function 
    categories($catid)    {
         global 
    $sph_messages,$showCats,$query,$mysql_table_prefix$cat_columns;
         if (
    $catid<>0)
             {
             
    $query1 "SELECT category FROM ".$mysql_table_prefix."categories WHERE category_id='$catid'";
             
    $result mysql_query($query1);
             
    $row mysql_fetch_array($result);
             
    $category $row['category'];
     
     
    //        print "<center><table width=\"550\"><tr><td>\n";
     //        print "$category\n";
             
    $query1 "SELECT parent_num FROM ".$mysql_table_prefix."categories WHERE category_id=$catid";
             
    $result mysql_query($query1);
             if (
    mysql_num_rows($result)> 0)
                 print 
    "<p class=\"cat1\">";
             
    $row mysql_fetch_array($result);
             
    $catid2 $row['parent_num'];
             
    $i=0;
             while (
    $catid2<>0) {
                 
    $query1 "SELECT category_id, category FROM ".$mysql_table_prefix."categories WHERE category_id='$catid2'";
                 
    $result mysql_query($query1);
                 
    $row mysql_fetch_array($result);
                 
    $catname[$i][1] = $row['category'];
                 
    $cid $catname[$i][2] = $row['category_id'];
                 
    $query1 "SELECT parent_num FROM ".$mysql_table_prefix."categories WHERE category_id='$cid'";
                 
    $result mysql_query($query1);
                 
    $row mysql_fetch_array($result);
                 
    $catid2 $row['parent_num'];
                 
    $i++;
             }
             print 
    "<a href=\"".$_SERVER['PHP_SELF']."\">".$sph_messages['Categories']."</a> > ";
             for (
    $x=count($catname)-1;$x>=0;$x--) {
                 print 
    "<a href=\"?catid=".$catname[$x][2]."\">".$catname[$x][1]."</a> > ";
             }
             print 
    "$category\n";
             
    $query1 "SELECT * FROM ".$mysql_table_prefix."categories WHERE parent_num=$catid ORDER BY category";
             
    $result mysql_query($query1);
             
    $rows mysql_num_rows($result);
             if (
    mysql_num_rows($result)> 0)
                 print 
    "<p><table width=\"100%\" class=\"catBanner\"><tr><td><b>".$sph_messages['Categories']."</b></td></tr></table></p>";
     
             
    $i=0;
             for (
    $x=1$x<=3$x++)
                 {
                 while (
    $i $rows/(3/$x) )
                     {
                     
    $row mysql_fetch_array($result);
                     
    $newcatid $row['category_id'];
                     
    $category $row['category'];
                     
    $query1 "SELECT count(*) FROM ".$mysql_table_prefix."site_category WHERE category_id=$newcatid";
                     
    $result2 mysql_query($query1);
                     
    $row2 mysql_fetch_row($result2);
                     
    $sites $row2[0];
                     print 
    "<a href=\"?catid=$newcatid\"><b>$category</b></a> ($sites)<br>\n";
                     
    $i++;
                     }
                 }
     
     
     
             
    $query1 "SELECT url, title, short_desc FROM ".$mysql_table_prefix."sites, ".$mysql_table_prefix."site_category WHERE category_id=$catid AND ".$mysql_table_prefix."sites.site_id=".$mysql_table_prefix."site_category.site_id order by title";
             
    $result mysql_query($query1);
             if (
    mysql_num_rows($result)> 0)
                 print 
    "<br><p><table width=\"100%\" class=\"webPageBanner\"><tr><td><b>".$sph_messages['Web pages']."</b></td></tr></table></p>";
             
    $num =;
             while (
    $row mysql_fetch_array($result)) {
                 
    $title $row['title'];
                 
    $url $row['url'];
                 
    $desc $row['short_desc'];
                 
    $num++;
                 print 
    "<b>$num.</b> <a href=\"$url\"><b>$title</b></a><br>$desc<br><font class=\"url\">$url</font><br><br>\n";
             }
             
    ?>
             <p>
             <hr class="powered" size="1">
             <?poweredby();?>
             </p><?
         
    } else {
     
             
    $query1 "SELECT * FROM ".$mysql_table_prefix."categories WHERE parent_num=0 ORDER BY category";
             
    $result mysql_query($query1);
             
    $rows mysql_num_rows($result);
     
             print 
    "<center><br>";
             if (
    $rows >0
                 print 
    "<b>".$sph_messages['CATEGORIES']."<b>";
             print 
    "<table width=\"550\" border=0 cellpadding=4>\n";
             if (
    $rows >0
                 print 
    "<tr><td colspan=$cat_columns><hr size=\"1\"></td></tr>";
             
    $j=0;
             while (
    $j $rows $cat_columns) {
                         print 
    "<tr>";
                 for (
    $k 0$k $cat_columns$k++) {
                     
    $width 100/$cat_columns;
                     print 
    "<td valign=\"top\" width=\"$width%\">";
                     if (
    $cat_columns $j $k $rows) {
                         
    $row mysql_fetch_array($result);
                         
    $catid $row['category_id'];
                         
    $category $row['category'];
                         
    $query1 "SELECT * FROM ".$mysql_table_prefix."categories WHERE parent_num=$catid";
                         
    $result2 mysql_query($query1);
                         print  
    "<a href=\"?catid=$catid\" class=\"cat1\">".$row['category']."</a><br>\n&nbsp;&nbsp;";
                         print 
    "<font size=-1>";
                         
    $i 0;
                         while ((
    $row2 mysql_fetch_array($result2)) && $i<3) {
                             if (
    $i != 0)
                                 print 
    ", ";
                             
    $id=$row2['category_id'];
                             print 
    "<a href=\"?catid=$id\">".$row2['category']."</a>";
                             
    $i++;
                         }
                         if (
    $i3)
                             print 
    "...";
                         print 
    "</font><br><br>";
                         print 
    "</td>";
                     }    
                 
                 }
                 print 
    "</tr>";
                 
    $j++;
             }
     
             print 
    "<tr><td colspan=$cat_columns>";
             if (
    $rows >0) {
                 print 
    "<hr size=\"1\">";
             }
             print 
    "</td></tr></table></center>";
             
    poweredby();
             }
     
         }
     
     
     
     function 
    dosearch($query$start$category$searchtype$results) {
         global 
    $sph_messages$results_per_page,
             
    $links_to_next,
             
    $show_query_scores,
             
    $mysql_table_prefix,
             
    $desc_length;
         if (
    $results != "") {
             
    $results_per_page $results;
         }
     
         if (
    $searchtype == "phrase") {
            
    $query=str_replace('"','',$query);
            
    $query "\"".$query."\"";
         }
     
         
    $starttime getmicrotime();
         
    // catch " if only one time entered
             
    if (substr_count($query,'"')==1){
                
    $query=str_replace('"','',$query);
             }   
         
    $words makeboollist($query);
         
    $ignorewords $words['ignore'];
         print 
    "<br>";
         if (
    count($ignorewords)>0) {
             print 
    "<center class=\"commonReport\">".$sph_messages["ignoredWords"]." ";
             while (
    $thisword=each($words['ignore'])) {
                 print 
    " ".$thisword[1];
             }
             print 
    "</center>";
         }
         if (
    $start==0
             
    $start=1;
         
    $result search($words$category$start$results_per_page$searchtype);
         
    $querystripslashes($query);
         
    $entitiesQuery htmlspecialchars($query);
         if (
    $result==0)
             print 
    "<table width=\"100%\" class =\"resultReport\"><tr><td>".$sph_messages["The search"]." \"$entitiesQuery\" ".$sph_messages["noMatch"].".</td></tr></table>";
     
         
    $endtime getmicrotime() - $starttime;
         
    $rows $result['results'];
         
    $time round($endtime*100)/100;
         
    $matchword $sph_messages["matches"];
         if (
    $rows == 1) {
             
    $matchword$sph_messages["match"];
         }
         
         
    $num_of_results count($result) - 2;
         if (
    $start 2)
             
    saveToLog(addslashes($query), $time$rows);
         
    $from = ($start-1) * $results_per_page+1;
         
    $to min(($start)*$results_per_page$rows);
         if (
    $result != && $from <= $to)
             print 
    "<table width=\"100%\" class =\"resultReport\"><tr><td>".$sph_messages["Results"]."  $from-$to".$sph_messages["of"].$rows $matchword ($time ".$sph_messages["seconds"].")</td></tr></table><br>";
         print 
    "<table class=\"results\"><tr><td>";
         if (
    $rows>0) {
             
    $maxweight $result['maxweight'];
             
    $i 0;
             while (
    $i $num_of_results && $i $results_per_page) {
                 
    $title $result[$i]['title'];
                 
    $url $result[$i]['url'];
                 
    $fulltxt $result[$i]['fulltxt'];
                 
    $pageSize $result[$i]['size'];
                 if (
    $pageSize!=""
                     
    $pageSize "- ".number_format($pageSize1)."kb";
                 
                 
                 
    $txtlen strlen($fulltxt);
                 if (
    $txtlen $desc_length) {
                     
    $places = array();
                     foreach(
    $words['hilight'] as $word) {
                         
    $tmp strtolower($fulltxt);
                         
    $found_in strpos($tmp$word);
                         
    $sum = -strlen($word);
                         while (!(
    $found_in =='')) {
                             
    $pos $found_in+strlen($word);
                             
    $sum += $pos;  //FIX!!
                             
    $tmp substr($tmp$pos);
                             
    $places[] = $sum;
                             
    $found_in strpos($tmp$word);
     
                         }
                     }
                     
    sort($places);
                     
    $x 0;
                     
    $begin 0;
                     
    $end 0;
                     while(list(
    $id$place) = each($places)) {
                         while (
    $places[$id $x] - $place $desc_length && $x+$id count($places) && $place strlen($fulltxt) -$desc_length) {
                             
    $x++;
                             
    $begin $id;
                             
    $end $id $x;
                         }
                     }
     
                     
    $begin_pos max(0$places[$begin] - 30);
                     
    $fulltxt substr($fulltxt$begin_pos$desc_length);
     
                     if (
    $places[$begin] > 0) {
                         
    $begin_pos strpos($fulltxt" ");
                     }
                     
    $fulltxt substr($fulltxt$begin_pos$desc_length);
                     
    $fulltxt substr($fulltxt0strrpos($fulltxt" "));
                     
    $fulltxt "...".$fulltxt."...";
                 }
     
                 
    $weight number_format($result[$i]['weight']/$maxweight*1002);
                 if (
    $title=='')
                     
    $title $sph_messages["Untitled"];
                 
    $regs = Array();
                 foreach(
    $words['hilight'] as $change) {
                     while (@
    eregi("[^\>](".$change.")[^\<]"" ".$title." "$regs)) {
                         
    $title eregi_replace($regs[1], "<b>".$regs[1]."</b>"$title);
                     }
     
                     while (@
    eregi("[^\>](".$change.")[^\<]"" ".$fulltxt." "$regs)) {
                         
    $fulltxt eregi_replace($regs[1], "<b>".$regs[1]."</b>"$fulltxt);
                     }
                     
    $url2 $url;
                     while (@
    eregi("[^\>](".$change.")[^\<]"$url2$regs)) {
                         
    $url2 eregi_replace($regs[1], "<b>".$regs[1]."</b>"$url2);
                     }
                 }
                 if (
    $show_query_scores == 0) {
                     
    $weight '';
                 } else
                     
    $weight '['.$weight.'%]';
     
                 
    $num $from $i;
                 print 
    "<b>$num.</b> $weight <a href=\"$url\"><font class=\"title\">$title</font></a><br><div class=\"description\">$fulltxt</div><font class=\"url\">$url2 $pageSize</font><br><br>\n";
                 
    $i++;
             }
         }
         
    $pages ceil($rows $results_per_page);
         
    $prev $start 1;
         
    $next $start 1;
         if (
    $from <= $to) {
             if (
    $pages 0)
                 print 
    "<center>".$sph_messages["Result page"].": ";
             if (
    $start<>1)
                 print 
    "<a href=\"".$_SERVER['PHP_SELF']."?query=".addmarks($query)."&start=".$prev."&search=1&results=$results_per_page&type=$searchtype\">".$sph_messages["Previous"]."</a> ";
             
    $firstpage $start $links_to_next;
             if (
    $firstpage 1$firstpage 1;
             
    $lastpage $start $links_to_next;
             if (
    $lastpage $pages$lastpage $pages;
     
             for (
    $x=$firstpage$x<=$lastpage$x++)
                 if (
    $x<>$start)    {
                     print 
    "<a href=".$_SERVER['PHP_SELF']."?query=".addmarks($query)."&start=$x&search=1&results=$results_per_page&type=$searchtype>$x</a> ";
                 }
             else
                 print 
    "<b>$x </b>";
             if (
    $next <= $pages)
                 print 
    "<a href=\"".$_SERVER['PHP_SELF']."?query=".addmarks($query)."&start=".$next."&search=1&results=$results_per_page&type=$searchtype\">".$sph_messages["Next"]."</a>";
         }
         if (
    $pages && $from <=$to) {
             print 
    "</center>";
             
    ?>
             
             <br>
             <p>
             <hr class="powered" size="1">
             <?poweredby();?>
             </p>
             </td></tr></table> <?
     
         
    } else {
             
    ?>
             </td></tr></table>
             <br>
             <p>
             <hr class="powered" size="1">
             <?poweredby();?>
             </p>
              <?
         
    }
     }
     
     function 
    poweredby () {
         global 
    $sph_messages;
         
    ?>
             <!--IF YOUR WANT TO REMOVE THIS, PLEASE DONATE TO THE PROJECT-->         
             <center><font size="-2"><?print $sph_messages['Powered by'];?> <a href="http://www.cs.ioc.ee/~ando/sphider/">Sphider</a></font></center>
     
         <?
     
    }
     
     
     function 
    saveToLog ($query$elapsed$results) {
             global 
    $mysql_table_prefix;
         
    mysql_query("insert into ".$mysql_table_prefix."query_log (query, time, elapsed, results) values ('$query', now(), '$elapsed', '$results')");
         echo 
    mysql_error();
     }
     
     switch (
    $search) {
         case 
    1:
             if (!isset(
    $type)) {
                 
    $type "";
             }
             if (!isset(
    $results)) {
                 
    $results "";
             }
             
    dosearch($query$start$category$type$results);
         break;
         default:
             
    categories($catid);
         break;
         }
     
     include 
    "$include_dir/index_footer.inc";
     
    ?>
    Thanks guys!!

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know, but I just implemented this nice search script in a site.

    http://www.gr0w.com/amos/growsearch/readme.php

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks climberusa. In my code I'm thinking I need to change
    PHP Code:
    extract(getHttpVars()); 
    Any ideas please?

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    change your form method from POST to GET....
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Mike,

    Tried that but no luck. Here's my form(in a separate page)...

    PHP Code:
     <form action="http://mysite/search/search.php" method="get">
     <
    label for="search" class="bold">Search:</label>
     <
    input type="text" name="query" id="search" />
     <
    br />
     <
    input type="submit" name="submit" value="Submit" />
     <
    br /><br />
     </
    form

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    The script might need the hidden field that is present in the original search form.
    PHP Code:
    <form action="http://mysite/search/search.php" method="get">
    <
    label for="search" class="bold">Search:</label>
    <
    input type="text" name="query" id="search" />
    <
    br />
    <
    input type="hidden" name="search" value="1">
    <
    input type="submit" name="submit" value="Submit" />
    <
    br /><br />

    </
    form
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant! Thanks Mike!!

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    No problem Kev. Glad to help

    Mike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

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