Page navigation in PHP

In this tutorial i will show you how to make a page navigation. A bit more advanced php technique, but definitely not hard.


In this tutorial I’m going to talk about page navigation, the little numbers you see on the bottom of the page, indicating what page you are on.

Page navigation like we’re going to talk about in a minute, is great for guestbooks, forums, downloads or just other types of lists.

Lets get started
First of all we need to know what number of entries we have in the table itself.

Code:

1
<?php
2

3
$noEntriesQuery 
mysql_query(“SELECT id FROM table”) or die(mysql_error());
4

5
// read the number
6
$noEntries mysql_num_rows($noEntriesQuery);
7

8
?>



This will work on most servers. There are other technique’s of how you count the entries, but they aren’t needed right now.

Now lets calculate how much pages we need, but before we do that we need to know how much entries we want to show each page.

I like the number of 25 with most lists, so I’ll use that.

Code:

1
<?php
2

3
$noEntriesQuery 
mysql_query(“SELECT id FROM table”) or die(mysql_error());
4

5
// read the number
6
$noEntries mysql_num_rows($noEntriesQuery);
7

8
// set page limit
9
$pageLimit 25;
10

11
// calculate the number of pages
12
$noPages ceil($noEntries $pageLimit);
13

14
?>



The noPages variable contains the max number of pages possible. When I have 80 shouts, it will tell me I have 4 pages, because 25 fits in 80 three times, and the last 5 will be located on the forth page.

Now we need to know on what page we are at the moment. We will check on with page the user is by using a $_GET value. When the $_GET value isn’t set, we will set the default page.

Code:

1
<?php
2

3
$noEntriesQuery 
mysql_query(“SELECT id FROM table”) or die(mysql_error());
4

5
// read the number
6
$noEntries mysql_num_rows($noEntriesQuery);
7

8
// set page limit
9
$pageLimit 25;
10

11
// calculate the number of pages
12
$noPages ceil($noEntries $pageLimit);
13

14
// current page
15
$currentPage 0;
16

17
// check if the $_GET value exists, if yes.. set the $currentPage to that value
18
if(isset($_GET['spag']) && 
19
    is_numeric
($_GET['spag']) && 
20
    $_GET
['spag'] > && 
21
    $_GET
['spag'] < $noPages){
22
    
23
    
// the test passed, set the current page
24
    
$currentPage $_GET['spag'];
25

26
}
27

28
?>



When I was thinking of this technique, I found the $_GET['spag'] < $noPages part a bit confusing. So I’ll explain it.

Its quite simple, the first page is 0, the second page is 1. So when having 80 entries, (just an example) your forth page will be numbered as page 3. That’s why you use $_GET['spag'] < $noPages.

Now we’re going to make a start number, to understand this, you first need to understand LIMIT *.* in SQL.

LIMIT *.* can choose what entries are chosen, when using LIMIT 0.10. The found entries 0 to 10 are read. When using LIMIT 20.10, it starts with reading 20, and ends at 30. So the first number is the start and the second number is the amount of rows you want.

For our page navigation we’re gonna use this too. When the user is on the 2th page. We want SQL to select the post 25 to 50. (25 entries at a time)

To calculate were we need to start, just multiply the currenPage with the pageLimit. So when you are on page 2, the currenPage variable contains 1. So 1 x 25 = 25. So on page 2 we’ll start with entry 25.

Code:

1
<?php
2

3
$noEntriesQuery 
mysql_query(“SELECT id FROM table”) or die(mysql_error());
4

5
// read the number
6
$noEntries mysql_num_rows($noEntriesQuery);
7

8
// set page limit
9
$pageLimit 25;
10

11
// calculate the number of pages
12
$noPages ceil($noEntries $pageLimit);
13

14
// current page
15
$currentPage 0;
16

17
// check if the $_GET value exists, if yes.. set the $currentPage to that value
18
if(isset($_GET['spag']) && 
19
    is_numeric
($_GET['spag']) && 
20
    $_GET
['spag'] > && 
21
    $_GET
['spag'] < $noPages){
22
    
23
    
// the test passed, set the current page
24
    
$currentPage $_GET['spag'];
25

26
}
27

28
// start number
29
$start $currentPage $pageLimit;
30

31
// the query for the current page
32
$sql "SELECT * FROM yable ORDER by id LIMIT ".$start.",".$pageLimit;
33

34
?>



Ok, so now we know how to select the right entries for the right page. Now we need to make some real navigation or else this would all be useless.

The navigation is quite simple. I will first show you an example, and then explain it to you.

Code:

1
<?php
2
for($a 0$a $noPages$a++){
3
    
if($currentPage == $a){ 
4
        
echo($a+1);
5
    
}else{
6
        
echo '<a href="'.$_SERVER['PHP_SELF'].'?spag='.$a.'">'.($a+1).'</a>';
7
    
}
8
    
if($a $noPages 1){
9
        
echo '-';
10
    
}
11
}
12
?>



Basically what we’re doing here is looping a number (the pages) until we reached the latest page. When echo’ing the page number add +1 with it. Because remember, the second page will be page 1, the third will be 2 etc.

When the loop is at the current page, it will just echo the number. But when the loop is not on the current page, it will post a number that is linked to the other pages.

And while the loop is on a page where the page is not the last page, ($a < $noPages – 1) it will also echo an “-“. Just because it looks nice.

This is the end of this tutorial. Please reply to on the bottom of the page when you don’t understand something.
Save this tutorial!

Digg! delicious StumbleUpon Furl this Spurl


Page navigation: 1
 By baig on 15-12-2007

please tell me how i can use $_get to find out where the user at that time,or what is the current location of the user.




 By Jim on 15-12-2007

You can use GET variables for navigating, for example:

index.php?page=contact

And in the code you can check if the GET variable is "contact" and then include the contact page. Eg:

if($_GET['page'] == "contact") {

include'contactform.php';

} elseif($_GET['page'] == "download") {

include'downloads.php';

} else {

include'startpage';

}

I don't know your level in PHP, but if you want to make this a bit more nice you can also use switch() to do this. You can check php.net for the switch() function.

If you have any more problems, don't hesitate opening a topic at the forums. :)




 By nux86 on 26-02-2008

can do you help me..

how i can maka page like this:

"first back 1 2 3 4 next last"

ex: i have 6 page n i want it view like this

"first back 1 2 3 4 next last" ==> first i open

"first back 2 3 4 5 next last" ==> when i in page 4 and clik next

"first back 3 4 5 6 next last" ==> when i in page 5 and clik next

"first back 1 2 3 4 next last" ==> when i clik first

"first back 3 4 5 6 next last" ==> when i clik last

can u help me??




 By Jim on 17-03-2008

I dont understand. Can you create a topic on the forums please?

(Sorry for the late reply!)




 By tieulong on 21-05-2008

Thank you for your tutorial!
But i have some confusing,"When using LIMIT 20.30, it starts with reading 20, and ends at 30.", i think it starts with reading 20 and ends at 50(20+30).Is it right?Please explain!




 By Jim on 26-05-2008

When using LIMIT 30,25. It will start at row30 and it will read the next 25 rows (so till 55).



Page navigation: 1

You are not logged in. To reply to this tutorial, please login. If you dont have an account yet, you can register here.

©Copyrights Combined Minds. All rights reserved 2006 - 2009 : Disclaimer