Pagination in PHP
If you are going to have a site where users can submit stuff, and the stuff is listed on one page, you definitely need pagination. Pagination scripts consist of two main parts: getting the correct records and displaying them, and displaying the page links at the bottom.
First, our data. We will use arrays in this example, but you can adapt it for MySQL.
<?php
$data = array(
1 => array('user' => 'Bob', 'comment' => 'I like your site!'),
2 => array('user' => 'George', 'comment' => 'I like your site more than he does!'),
3 => array('user' => 'Tom', 'comment' => 'I like your site, but mine\'s better.'),
4 => array('user' => 'Alex', 'comment' => 'This site is awful. Mine is MUCH better.'),
5 => array('user' => 'Admin', 'comment' => 'Stop insulting my site!! I made $25,000 last month!!!'),
6 => array('user' => 'Harry', 'comment' => 'Woof!'),
);
Next, figure out which posts need to be shown, and cut off the rest.
$perpage = 2; //Obviously you would want to change this to something higher, depending on the content. if(isset($_GET['start'])) $start = $_GET['start']; else $start = 0; $numposts = count($data); $data = array_slice($data, $start, $perpage);
Now that the data is what we want, we can output it (this code will need to be replaced with your own.)
foreach($data as $k => $v)
{
echo $data[$k]['user'].' said: '.$data[$k]['comment'].'<br/>'."\n";
}
The last step is to show the next page/previous page links.
if($start > 0)
{
$text .= '<a href="guestbook.php?start='.($start - $perpage).'">< Previous Page </a>';
}
if($start > 0 && $numposts > $perpage && $start < $numposts - $perpage)
{
$text .= ' | ';
}
if($numposts > $perpage && $start < $numposts - $perpage)
{
$text .= '<a href="guestbook.php?start='.($start + $perpage).'">Next Page ></a>';
}
echo $text;
And we’re done! When a user clicks the links at the bottom, $start is added to or subtracted from. If $_GET['start'] is not set (the user has just entered this site area and hasn’t changed the page yet) it is set to 0, so the script will show the posts starting at the beginning.
If you have any comments, please share ![]()

September 13th, 2008 at 2:41 pm
please add the following after the last if statement:
echo $text;
September 16th, 2008 at 7:15 pm
Oops, sorry about that. I ripped this out of my CMS where $text is the variable containing the page content, and is printed later in the script.
October 27th, 2008 at 9:24 am
Thanks a bundle for the script. Works great with my guestbook!
I modified it so it reads a file (to where all the entries are written) into an array using "file()" (I haven’t got mysqlDB) and it works like a charm!
You wouldn’t happen to have a tip on how to number the pages instead of just giving the option of next and previous page? (e.g )
I thought mabye
if($start = 0)
{
$text .= ‘1|<a href="guestbook.php?start=’.($start + $perpage).’" rel="nofollow">2</a>|<a href="guestbook.php?start=’.($start + $perpage + $perpage).’" rel="nofollow">3</a>’;
}
elseif($start = $perpage)
{
$text .= ‘<a href="guestbook.php?start=’.($start - $perpage).’" rel="nofollow">1</a>|2|<a href="guestbook.php?start=’.($start - $perpage).’" rel="nofollow">3</a>|<a href="guestbook.php?start=’.($start + $perpage + $perpage).’" rel="nofollow">4</a>’;
}
elseif($start = $perpage + $perpage)
….etc.
But this seems a bit more laborous than it should…
Any bright thoughts?
October 27th, 2008 at 4:55 pm
This probably shows all the numbers (I haven’t tested it though).
$numpages = ceil($numposts / $perpage);
for($i = 1; $i < $numpages + 1; $i++)
{
if($i = 1)
$start = 0;
else
$start = $perpage * i;
echo ‘<a href="guestbook.php?start=’.$start.’" rel="nofollow">’.$i.’</a> ‘;
}
I made another pagination code a few years ago that had numbers, but I have no idea what it is.
April 13th, 2009 at 6:41 am
at first i thought this tutorial was useful but left very little for expansion. not to mention the method is a little wrong.. because you should sort it by pages not posts.. pages are the key to linking all of the posts and the range per page, together.
i later found a different method. using the math of
$totalpages = ceil($sqltotalposts['id'] / $limit);
and
$set_limit = $page * $limit - ($limit-1);
which made a lot more sense and worked a lot more fluidly.
sorry for the negative comment but i think this tutorial contains bad methods.
May 23rd, 2009 at 5:43 am
Works great !
Thanks !
____________________
Saguenay-IT, (IT Outsourcing, SOA, PHP, ASP, Flex, ActionScript, JavaScript…)