kikilahooch Posted September 4, 2006 Share Posted September 4, 2006 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 parameterif (!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 matchif (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 alternativeif ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" 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 setecho' <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.) $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 " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  "; }// 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 " <a class=orange href=\"$PHP_SELF?s=$news&q=$var\"><b>Next 10 >></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] Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
acdx Posted September 4, 2006 Share Posted September 4, 2006 Use [url=http://php.net/manual/en/function.mysql-data-seek.php]mysql_data_seek()[/url] ;) Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 actually, use the keyowrd LIMIT in your statements, not mysql_data_seek. Quote Link to comment Share on other sites More sharing options...
acdx Posted September 4, 2006 Share Posted September 4, 2006 EDIT: oops, you're right Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.