A very usefull and common subject : php pagination. You need this when you have a lot of entries in the database and don’t want to show them in a single page, here comes the results pagination in php. This works pretty simple, not that complex : firstly we calculate the total ammount of entries, then the numbers of page needed to show everthing, and, of course, how many results per page. Finaly, we’ll print out the entries and links for next, previous and page numbers.
//include database connection (check previous posts to get this one)
include('dbConnection.php');
//get the number of total rows
$query = "SELECT * FROM TableName";
$result = mysql_query($query);
// Number of records found
$num_record = mysql_num_rows($result);
// Number of results per page
$display = 5;
if(isset($_GET['page']) {
$currentPage = $_GET['page'];
}else{
$currentPage = 1;
}
//last page
$lastPage = ceil($num_record/$display);
//limit in the query thing
$limitQ = 'LIMIT ' .($currentPage - 1) * $display .',' .$display;
//normal query and print results
$query = "SELECT * FROM TableName $limitQ";
$result = mysql_query($query);
//here you do your loop like
while($row=@mysql_fetch_object($result)) {
print "$row->FieldName";
}
//pagination navigation (links)
//previous
if ($currentPage == 1) {
print "Prev ";
} else {
print "<a href=pagename.php?page=1>First page</a> ";
$previousPage = $currentPage-1;
print "<a href=pagename.php?page=$previousPage>Previous</a>";
}
print " { Page $currentPage of $lastPage } ";
//for next pages links
if ($currentPage== $lastPage) {
print "Next last";
} else {
$nextPage = $currentPage+1;
print " <a href=pagename.php?page=$nextPage>NEXT</a> ";
print " <a href=pagename.php?page=$lastPage>LAST</a> ";
}
?>
Now you can use this pagination to split your mysql results into multiple webpages!