Jump to content

I want to display a limited amount of results per page


kikilahooch

Recommended Posts

I have a search facility on my page so that users can search for products. There are a lot of products on my site and searchs can return up to 100 results some time. I want to be able to limit the amount of results to 10 per page. The code below does this but when I click on display next results it just displays the same ones back again. Would be really grateful if someone could point out what needs to be modified

[code]
<?php

$host=

  // Get the search variable from URL
  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10;

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p><b>Please enter a search...<B></p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p><b>We dont seem to have a search parameter!<b></p>";
  exit;
  }

include "db.php";

$query = "select * from product where dept like \"%$trimmed%\" OR type like \"%$trimmed%\" OR prodName like \"%$trimmed%\" OR shopName like \"%$trimmed%\" OR brand like \"%$trimmed%\" order by dept, type, prodName, shopName, brand";



//db
$result = mysql_query($query,$conn) or die(mysql_error());

//get the number of rows in the result set; should be 1 if a match
if (mysql_num_rows($result) >= 1) {
//if authorized, get the values of name, surname
$image = mysql_result($result, 0, 'image');
$prodName = mysql_result($result, 0, 'prodName');
$price = mysql_result($result, 0, 'price');



$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

// google
echo "<p><a href=\"http://www.google.com/search?q="
  . $trimmed . "\" target=\"_blank\" title=\"Look up
  " . $trimmed . " on Google\">Click here</a> to try the
  search on google</p>";
  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");


// begin to show results set
echo'


<h1><font color="#FF6600"><center>SEARCH RESULTS</center></font></h1>



<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">
<tr>


<td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Image</b></center></td>
<td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Product Name</b></td>
<td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Price</b></td>
<td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Buy</b></td>



</tr>';
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo'<tr>


<td align="center" width="150" height="200"><img src="http://snet.wit.ie/~ciaracousins/clothes/' . $row['image'] . '">
<td align="center"><b>'.$row['prodName'].'</td></b>
<td align="center"><b>€'.$row['price'].'</td></b>

<td align="center"><form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" border="0" name="submit"></td>





</tr>';
}
echo'</table>';
}

$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["1st_field"];

  echo "$count.)&nbsp;$title" ;
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
  Prev 10</a>&nbsp&nbsp;";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo "&nbsp;<a class=orange href=\"$PHP_SELF?s=$news&q=$var\"><b>Next 10 &gt;&gt;</b></a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p><b>Showing results $b to $a of $numrows</b></p>";
 








$imageDir = "http://snet.wit.ie/~ciaracousins/clothes/";
$imagePath = $imageDir."".$image;

//echo "$query";
?> [/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.