Publishing System Settings Logout Login Register
Pagination! Add pagination to your site
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on November 27th, 2006
11665 views
PHP Coding
Welcome to my tutorial all about pagination.

Firstly, What is pagination?

Pagination is very good for handling hundreds of results. Imagine Pixel2Life, they have over 20,000 tutorials indexed. If they was to put every single one, onto 1 page. Imagine how long that page would be. But with pagination, you can limit results per page and have as many pages as you need.

Pixel2Life Pagination Example:



Thats how Pixel2Life do it. They limit the amount of pages shown aswell, thats a bit complicated for this tutorial so I will write another in the near future.

Lets move on, and start building our pagination code!



When using pagination, you obviously need something to paginate.

Lets take for example, we had a tutorial system. We want to limit each page to 20 results, and then show pages of results.

To do this, we need to define the page number in our URL. So maybe something like:

http://www.yoursite.com/index.php?view=tutorials&page=3


Now we need to check, and see if the page number is defined in the URL.
This is the start of our code!

[code=PHP]<?php
    if($_GET['page']) // Is page defined?
    {
        $page = $_GET['page']; // Set to the page defined
    }else{
        $page = 1; // Set to default page 1
    }[/code]

So, if page is defined in the URL we set the variable $page as the page defined in the URL. If page is not defined, we set the page to 1 which will show the first set of results.

Now lets set the maximum results per page. For this tutorial, I have set to 10. You can change it.

[code=PHP]$max = 10; // Set maximum to 10[/code]

Now we have set the maximum, and done the code to check what page were on. Lets work out what results to show, using maths.

[code=PHP]$cur = (($page * $max) - $max); // Work out what results to show[/code]

So firstly, we multiply the page number with the maximum results to show, then we subtract the maximum results. That will give us, what results to show.


Now we need to do our query, that will select the correct data from the database.

[code=PHP]
$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[/code]

Now, if you want to know what that query means, and how to do it yourself. Take a read of my other tutorial found Here

For the parts not explained in my other tutorial, here:

the LIMIT $cur, $max is simple to understand. It only brings out the $cur record... So if $cur = 5, it would bring the 5th record and on.... The $max limits the results. Which in our case is 10, so it would bring results 5 - 15. Simple stuff huh?

Now we need to know, how many records in the table. So we do a little query, using mysql_query and mysql_num_rows.

[code=PHP]$counttotal = mysql_query("SELECT * FROM `table` ") or die(mysql_error()); // select all records       
$counttotal = mysql_num_rows($counttotal); // count records[/code]


$counttotal will now give us the amount of results in that table.



Now we need to work out, how many pages there is going to be.

We can do this, by some division and rounding the answer up using ceil

[code=PHP]$total_pages = ceil($counttotal / $max); // dive the total, by the maximum results to show [/code]

Were nearly there! Dont give up yet.

So now we have worked out, how many pages there are going to be.

Lets start echoing out the page numbers.

[MY_P2L_AD]

[code=PHP]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
                } [/code]
I have commented the code above, very easy to understand. The above code would echo this:

� Previous


Now lets get the page numbers done...

[code=PHP]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
                        }
                } [/code]

Again, the code is commented. for each page, we echo a result. If we are on that page, instead of showing a link we show a bold page number with no link.

Now lets make a next page link..

[code=PHP]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[/code]

Now if there was a next page, you would get a link something like this:

Next �

and thats it! Simple when you know how to do it :)



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

[code=PHP]<?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[/code]

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!
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Adam

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top