php dynamic search engine script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bryanau
Forum Newbie
Posts: 1
Joined: Mon Jun 30, 2008 4:29 pm

php dynamic search engine script

Post by bryanau »

Hello Everyone,

I have a fully functional php search engine script that dynamically creates more pages depending on the number of items found in the database. However I am unable to type in multiple keywords and have that find anything. Below is the php code:

Code: Select all

<?php
###############################
#
#
# PHP Search Engine/Navigation Script
# by: Bryan Alexander
# Use by http://www.phoxproducts.com
#
#
###############################
 
   //load settings
   if (!isset($_CONFIG)){
     require '../config.php';
     require '../connectdb.php';}    
     
    //creates dynamic GET variables
    $hunt = $_GET['hunt'];
    $category = $_GET['category'];
    $limit = $_GET[limit]; 
    $page = $_GET[page];
    
    //if hunt box is empty and category is empty
    if ($hunt == NULL && $category == NULL) {
        echo "<div id='searchNum'>You did not enter an item, please go back and try again.</div>";
        }  else {
 
    //default results per-page.
    if (!($limit)){
     $limit = 2;}
     
    //default page value.
    if (!$page || $page < 0){
     $page = 0;}
        
    //if hunt box has content
    if ($hunt != NULL && $category == NULL) {
    
        include('cleaner.php');
        include('stemmer.php');
         
    $sql = "SELECT DISTINCT * FROM products WHERE category LIKE '%".$hunt."%' OR brand LIKE '%".$hunt."%' OR model LIKE '%".$hunt."%' ORDER BY price DESC";
 
                $result = mysql_query($sql) or die(mysql_error());
        
        } else {
        
        //sql query
        $result = mysql_query("SELECT * FROM products WHERE category LIKE '%".$category."%' OR brand LIKE '%".$category."%'")
        or die(mysql_error());}
    
        //turns counts number of items in array
        $count = mysql_num_rows($result);
        
        //number of results pages.
        $pages = intval($count/$limit);
 
        //$pages now contains int of pages, unless there is a remainder from division.
        
        //has remainder so add one page
        if ($count % $limit) {
        $pages++;}
        
        //current page number.
        $current = ($page/$limit) + 1;
        
        //if $pages is less than one or equal to 0, total pages is 1.
        if (($pages < 1) || ($pages == 0)) {
        $total = 1;}
        
        //else total pages is $pages value.
        else {
        $total = $pages;}
        
        //the first result.
        $first = $page + 1;
        
        //if not last results page, last result equals $page plus $limit.
        if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
        $last = $page + $limit;} 
        
        //if last results page, last result equals total number of results. 
        else{
        $last = $count;}
        
        ?>
        <!--Search Result Numbers -->
        <?php if ($hunt != NULL) { ?><center><h2>Search Results for '<?=$hunt?>'</h2></center><?php } ?>
        <table width="100%" border="0">
         <tr>
          <td width="50%" align="left">
        Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$count?></b>
          </td>
          <td width="50%" align="right">
        Page <b><?=$current?></b> of <b><?=$total?></b>
          </td>
         </tr>
         <tr>
          <td colspan="2" align="right">&nbsp; 
          </td>
         </tr>
         <tr>
          <td colspan="2" align="right">
        Results per-page:
        <? if ($hunt != NULL && $category == NULL) { ?>
        <a href="<?=$PHP_SELF?>?hunt=<?=$hunt?>&page=<?=$page?>&limit=5">5</a> | 
        <a href="<?=$PHP_SELF?>?hunt=<?=$hunt?>&page=<?=$page?>&limit=10">10</a> | 
        <a href="<?=$PHP_SELF?>?hunt=<?=$hunt?>&page=<?=$page?>&limit=20">20</a> | 
        <a href="<?=$PHP_SELF?>?hunt=<?=$hunt?>&page=<?=$page?>&limit=50">50</a>
        <? }  else {?>
        <a href="<?=$PHP_SELF?>?category=<?=$category?>&page=<?=$page?>&limit=5">5</a> | 
        <a href="<?=$PHP_SELF?>?category=<?=$category?>&page=<?=$page?>&limit=10">10</a> | 
        <a href="<?=$PHP_SELF?>?category=<?=$category?>&page=<?=$page?>&limit=20">20</a> | 
        <a href="<?=$PHP_SELF?>?category=<?=$category?>&page=<?=$page?>&limit=50">50</a>
        <? } ?>
          </td>
         </tr>
        </table>
        <?php
        
        if ($category == NULL && $hunt != NULL) {
        // Now we can display results.
        $results = mysql_query("SELECT * FROM products WHERE category LIKE '%".$hunt."%' OR brand LIKE '%".$hunt."%' OR model LIKE '%".$hunt."%' ORDER BY price ASC LIMIT $page, $limit");
        
        //if results are produced
        if ($count > 0) {
    
            echo "<div id='searchNum_found'>".$count." result(s) found with '<b>".$hunt."</b>'</div>";
            echo "<br><br>";}
        } else {            
            $results = mysql_query("SELECT * FROM products WHERE category LIKE '%".$category."%' OR brand LIKE '%".$category."%' ORDER BY price ASC LIMIT $page, $limit");}
            
            
        //individual items printed
        while ($data = mysql_fetch_array($results))
        {
        ?>
             <table width="530" height="150" cellpadding="2" id="prodList">
                    <tr>
                    <td align="left" id="prod_title" colspan="2">
                    <a href="http://de" title="<?=$data["title"]?>"><?=$data["brand"]?> <?=$data["model"]?></a>
                    </td>
                    </tr>
                 <tr>
                     <th width="110" height="100" scope="col" border="1" bgcolor="#cccccc"><?=$data["img_thumb"]?></th>
                     <th width="313" scope="col" align="left" valign="top" id="prod_desc">
                    <b>Category:</b> <?=$data["category"]?><br>
                    <b>Brand:</b> <?=$data["brand"]?><br>
                    <b>Model:</b> <?=$data["model"]?><br>
                    <b>Product ID#:</b> <?=$data["prod_id"]?><br></th>
                 </tr>
                 <tr>
                     <th height="20" scope="col" >Reviews[#]</th>
                     <th scope="col" align="right" id="prod_price">$<?=$data["price"]?></th>
                 </tr>
                 <tr>
                     <th height="10" scope="col" align="left">&nbsp;</th>
                     <th scope="col" align="right" id="prod_buy"><a href="#"><img src="../../_images/capture_btn.jpg" alt="Capture" border="1px solid #CCCCCC"/></a><br></th>   
                 </tr>
                 <tr></tr>
                 </table>
        <?php
        }
        ?>
        <div id="numPages">
        <p align="center">
        <?php
        if ($hunt != NULL) {
        //don't show back link if current page is first page.
        if ($page != 0) {
        $back_page = $page - $limit;
        echo("<a href=\"$PHP_SELF?hunt=$hunt&page=$back_page&limit=$limit\">back</a>    \n");}
        
        //loop through each page and give link to it.
        for ($i=1; $i <= $pages; $i++)
        {
         $ppage = $limit*($i - 1);
         if ($ppage == $page){
        
         //if current page don't give link, just text.
         echo("<b>$i</b>\n");}
         else{
         echo("<a href=\"$PHP_SELF?hunt=$hunt&page=$ppage&limit=$limit\">$i</a> \n");}
        }
        
        //if last page don't give next link.
        if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) {
        $next_page = $page + $limit;
        echo("<a href=\"$PHP_SELF?hunt=$hunt&page=$next_page&limit=$limit\">next</a>");}
        }
        
        if ($category != NULL) {
        //don't show back link if current page is first page.
        if ($page != 0) {
        $back_page = $page - $limit;
        echo("<a href=\"$PHP_SELF?category=$category&page=$back_page&limit=$limit\">back</a>    \n");}
        
        //loop through each page and give link to it.
        for ($i=1; $i <= $pages; $i++)
        {
         $ppage = $limit*($i - 1);
         if ($ppage == $page){
        
         //if current page don't give link, just text.
         echo("<b>$i</b>\n");}
         else{
         echo("<a href=\"$PHP_SELF?category=$category&page=$ppage&limit=$limit\">$i</a> \n");}
        }
        
        //if last page don't give next link.
        if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) {
        $next_page = $page + $limit;
        echo("<a href=\"$PHP_SELF?category=$category&page=$next_page&limit=$limit\">next</a>");}
        }
        ?>
        </p>
        </div>
        <?php } ?>
If anyone has any ideas that would be helpful, please respond. Thank you.
Post Reply