I had loads of trouble finding something that worked for me so I wrote this, it's almost certainly not the best code you'll ever come across but it works.
If anybody has some tips on how to improve it please let me know. It's not the full code but just the bits that matter....
the search function passes :
$search (search value)
$n (number of records per page)
// initialise offset value
if(!isset($offset)) {
$offset = 0;
$count = 0;
}
//---- code to count total number of rows goes here -------//
// Records found
if ($count) {
// set up prev/next stuff
$start = ($offset + 1) ;
$end = ($offset + $n) ;
$offset1 = $offset ;
$last = "OFF";
$first = "OFF";
if ($offset == 0) {
$first = "ON";
}
if ($end > $count) {
$last = "ON";
$end = $count;
}
// print total number and current page
echo "SEARCH RESULTS<BR><BR>$count records were found. Displaying $start - $end<BR><BR>";
// get records
$result = mysql_query("SELECT * FROM table WHERE search = '$search' ORDER BY priority LIMIT $offset,$n",$db);
while ($myrow = mysql_fetch_array($result)) {
//---- code to build output goes here --------------------//
}
// display Prev/Next as appropriate
if($first == "OFF") {
$offset = ($offset1 - $n) ;
print "<a href=\" " . $PHP_SELF . "?offset=" . $offset . "&n=" . $n . " \">Prev Page</a> ";
}
if($last == "OFF") {
$offset = ($offset1 + $n) ;
print "<a href=\" " . $PHP_SELF . "?offset=" . $offset . "&n=" . $n . " \">Next Page</a>";
}
// no records found
}else{
echo "No records were found , please try again.";
}