Jump to content

PHP Dynamic Search Engine Script not working


bryanau

Recommended Posts


Hello everyone,

 

Below is the code for a dynamic php script that works perfectly, however if i type in two words it produces 0 results. Any feedback on this issue would be greatly appreciated.

<div id="middle_col">
<?php
###############################
#
#
# PHP Search Engine/Navigation Script
# by: Bryan Alexander
# Use by 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

;

//trim whitespace from variable
$hunt = trim($hunt);

//if ($hunt != NULL && $category == NULL) {
	//$searched = mysql_query("INSERT INTO search (searched) VALUES ('$hunt')");} 


//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) {	

	//sql query
	$result = mysql_query("SELECT DISTINCT * FROM products WHERE category LIKE '%".$hunt."%' OR brand LIKE '%".$hunt."%' OR model LIKE '%".$hunt."%'")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">  
          </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"> </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 } ?>
<!-- END OF SEARCH ENGINE SCRIPT -->
</div>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.