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.
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.
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.
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.
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.
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.
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.
| php | |
| 1 2 3 4 5 6 7 8 | <?php $noEntriesQuery = mysql_query("SELECT id FROM table") or die(mysql_error()); // read the number $noEntries = mysql_num_rows($noEntriesQuery); ?> |
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.
| php | |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $noEntriesQuery = mysql_query("SELECT id FROM table") or die(mysql_error()); // read the number $noEntries = mysql_num_rows($noEntriesQuery); // set page limit $pageLimit = 25; // calculate the number of pages $noPages = ceil($noEntries / $pageLimit); ?> |
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.
| php | |
| 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 | <?php $noEntriesQuery = mysql_query("SELECT id FROM table") or die(mysql_error()); // read the number $noEntries = mysql_num_rows($noEntriesQuery); // set page limit $pageLimit = 25; // calculate the number of pages $noPages = ceil($noEntries / $pageLimit); // current page $currentPage = 0; // check if the $_GET value exists, if yes.. set the $currentPage to that value if(isset($_GET['spag']) && is_numeric($_GET['spag']) && $_GET['spag'] > 0 && $_GET['spag'] < $noPages){ // the test passed, set the current page $currentPage = $_GET['spag']; } ?> |
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.
| php | |
| 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 | <?php $noEntriesQuery = mysql_query("SELECT id FROM table") or die(mysql_error()); // read the number $noEntries = mysql_num_rows($noEntriesQuery); // set page limit $pageLimit = 25; // calculate the number of pages $noPages = ceil($noEntries / $pageLimit); // current page $currentPage = 0; // check if the $_GET value exists, if yes.. set the $currentPage to that value if(isset($_GET['spag']) && is_numeric($_GET['spag']) && $_GET['spag'] > 0 && $_GET['spag'] < $noPages){ // the test passed, set the current page $currentPage = $_GET['spag']; } // start number $start = $currentPage * $pageLimit; // the query for the current page $sql = "SELECT * FROM yable ORDER by id LIMIT ".$start.",".$pageLimit; ?> |
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.
| php | |
| 1 2 3 4 5 6 7 8 9 10 11 12 | <?php for($a = 0; $a < $noPages; $a++){ if($currentPage == $a){ echo($a+1); }else{ echo '<a href="'.$_SERVER['PHP_SELF'].'?spag='.$a.'">'.($a+1).'</a>'; } if($a < $noPages - 1){ echo '-'; } } ?> |
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.
discuss this topic to forum
