Jump to content


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


I want to display a limited amount of results per page

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



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

// rows to return

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

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

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";

$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');


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

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

// begin to show results set

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

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

<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>

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

<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>


$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
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
  Prev 10</a>&nbsp&nbsp;";

// calculate number of pages needing links

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

  if ($numrows%$limit) {
  // has remainder so add one page

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

  // not last page so give NEXT link

  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]

Share this post

Link to post
Share on other sites
first, learn to sanitise your user input for mysql (hint: read up on mysql_real_escape_string)

2nd, search for Pagination, plenty of reading material.

Share this post

Link to post
Share on other sites
Use [url=http://php.net/manual/en/function.mysql-data-seek.php]mysql_data_seek()[/url] ;)

Share this post

Link to post
Share on other sites
actually, use the keyowrd LIMIT in your statements, not mysql_data_seek.

Share this post

Link to post
Share on other sites


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.