Hi ,
I've been trying to set up next and previous buttons for the search results of stockists -
It's all set up fine with the first 5 records displaying, but then when I
click on the other page links, the whole 2000 records are brought back from
the database.
I know that this is because my Sql statement uses a variable passed in from
a form, and when the php is run again to get the next/prev pages, this
variable is not being sent with it.
Can anyone please look at my script and tell me what i'm missing out - I'd be
really grateful as I'm new to php and need all the help I can get!
This is the relevant bit of code from my html form where the user enters in
their postcode:
<FORM ACTION="search.php" METHOD="post"
onsubmit="this.postcode.optional = false;
return verify(this);">
<div >
<table border="0 " width="500">
<tr>
<td> </td>
</tr>
<tr>
<td align = "right">
<b>Enter your postcode:
</b></td>
<center>
<td valign="middle"><input type="text" name="postcode" size="20"
tabindex="1"></td></center></tr>
this is the code for 'search.php':
<head>
<title>Stockist search results</title>
</head>
<body>
<TABLE border =0 cellpadding = "4" width ="550">
<tr>
<?php
echo "<td colspan='4' align='left' valign='top'><h2>Stockists listed for
  <font color = '#000000'>".$postcode.": </font></h2></td>";
?>
<td colspan="4" align="right" valign="top"><a
href="javascript:history.back();"><font size = "-1">Back</font></a></td>
</tr>
<?php
// Connecting, selecting database
$link = mysql_connect("db", "user", "password")
or die("Error: Could not connect to the database. Please try again
later.");
mysql_select_db("dbname",$link);
$limit = 5;
// get the number of rows returned
$result = mysql_query("SELECT * from stockists2 WHERE stockists2.postcode
like '$postcode%'",$link);
$num_results = mysql_num_rows($result);
echo "<tr><td colspan='8' align='center'><h3>Number of search results:
".$num_results."</h3></td></tr>";
// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=0;
}
$result = mysql_query("SELECT * from stockists2 WHERE stockists2.postcode
like '$postcode%' order by town asc limit $offset, $limit",$link);
// now display the results returned
while ($data = mysql_fetch_array($result))
{
//do stuff to display results
}
// next we need to do the links to other results
// bypass PREV link if offset is 0
if ($offset >0)
$prevoffset=$offset-5;
$urlprev = "$PHP_SELF?offset=$prevoffset";
print "<a href='$urlprev'>PREV</a> \n";
}
// calculate number of pages needing links
$pages=intval($num_results/$limit);
// $pages now contains int of pages needed unless there is a remainder from
division
if ($num_results%$limit) {
// has remainder so add one page
$pages++;
}
for ($i=1;$i<=$pages;$i++) { // loop thru to print page numbers
$newoffset=$limit*($i-1);
$urlnext = "$PHP_SELF?offset=$newoffset";
print "<a href='$urlnext'>$i</a> \n";
}
// check to see if last page
if (!(($offset/$limit+1)==$pages) && $pages!=1) {
// not last page so give NEXT link
$newoffset=$offset+5;
$urlnext = "$PHP_SELF?offset=$newoffset";
print "<a href='$urlnext'>NEXT</a><p>\n";
}
?>
</body>
Thanks very much in advance