Pagination! Add pagination to your site
Views: 7587
Comments: 8
Posted: 11-27-2006
based on 19 votes.
Faves: 19 | + Faves
Page(s): 1 2 3 4


Now, we have it all done. Hopefully you understand it all. Here is the code, in full.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
   
if($_GET['page']) // Is page defined?
   
{
       
$page = $_GET['page']; // Set to the page defined
   
}else{
       
$page = 1; // Set to default page 1
   
}
$max = 10; // Set maximum to 10
 
$cur = (($page * $max) - $max); // Work out what results to show
 
$getdata = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT $cur, $max") or die(mysql_error()); // select the results
$data = mysql_fetch_array($getdata); // get the data
 
$counttotal = mysql_query("SELECT * FROM `table` ") or die(mysql_error()); // select all records       
$counttotal = mysql_num_rows($counttotal); // count records
 
$total_pages = ceil($counttotal / $max); // dive the total, by the maximum results to show
 
if($page > 1){ // is the page number more than 1?
               
$prev = ($page - 1); // if so, do the following. take 1 away from the current page number
               
echo '<a href="?page=' . $prev . '">« Previous</a>'; // echo a previous page link
               
}
 
for($i = 1; $i <= $total_pages; $i++) // for each page number
               
{
                   
if($page == $i) // if this page were about to echo = the current page
                       
{
                           
echo'<b>' . $i .'</b> '; // echo the page number bold
                               
} else {
                           
echo '<a href="?page=' . $i . '">' . $i . '</a> '; // echo a link to the page
                       
}
               
}
 
if($page < $total_pages){ // is there a next page?
                   
$next = ($page + 1); // if so, add 1 to the current
               
echo '<a href="?page=' . $next . '">Next »</a>'; // echo the next page link
                   
}
?> // end php script

And thats it. :)

Hope you enjoyed this tutorial, and learnt something if you did not before know how to do this. Please comment this tutorial.

If you need help, post a topic in the help forum and someone will gladly help you!

Page(s): 1 2 3 4
. Adam . - http://
Lawl?
Close