I've written pagination before but it's been a long while so I had to use a refresher tutorial. In the end, here's my code:
// Select correct database and query it
$db_selected = mysql_select_db("database",$connect);
if(!$db_selected){
die("Cannot connect to database: " .mysql_error());
}else{
$query = "SELECT * FROM table_name WHERE column = 'x'";
// Check to see if there is already a page 1
if(!isset($pagenum)){
$pagenum = 1;
}
$result = mysql_query($query);
$rows = mysql_num_rows($result);
$page_rows = 4;
$last = ceil($rows/$page_rows);
// Pagination information
if ($pagenum < 1){
$pagenum = 1;
}elseif($pagenum > $last){
$pagenum = $last;
}
$max = " LIMIT ".($pagenum - 1) * $page_rows.", ".$page_rows."";
$query2 = $query.$max;
$result2 = mysql_query($query2);
}
And my problem is that it displays the correct number of table rows on the first page but clicking on the links to the subsequent pages yields no results. For example, this query returns 6 results, the first 4 are shown so clicking on the "next" or "last" page links should show the last 2 but they don't. Only the first four remain displayed. 😕