Pernah menggunakan search engine???? pasti kita semua pernah menggunakannya, apa yang terjadi apabila hasil yang kita cari mencapai angka lebih dari 10000 ??? apa yang kita cari dimunculkan semuanya??? tentu saja tidak, paling tidak akan dikelompokkan menjadi beberapa halaman. nah apa, pertanyaanya bagaimana membuat pengelompokan halaman menggunakan PHP dan MySql seperti itu???? berikut ini ada caranya.... |
<?php // how many rows to show per page // if $_GET['page'] defined, use it as page number // counting the offset $query = " SELECT val FROM randoms " . // ... more code here Paging is implemented in MySQL using LIMIT that take two arguments. The first argument specifies the offset of the first row to return, the second specifies the maximum number of rows to return. The offset of the first row is 0 ( not 1 ). When paging.php is called for the first time the value of $_GET['page'] is not set. This caused $pageNum value to remain 1 and the query is : SELECT val FROM randoms LIMIT 60, 20 this query returns rows 60 to 79. After showing the values we need to print the links to show any pages we like. But first we have to count the number of pages. This is achieved by dividing the number of total rows by the number of rows to show per page : $maxPage = ceil($numrows/$rowsPerPage); |
<?php // how many rows we have in database // how many pages we have when using paging? // print the link to access each page // ... still more code coming The mathematical function ceil() is used to round up the value of $numrows/$rowsPerPage. In this case the value of total rows $numrows is 295 and $rowsPerPage is 20 so the result of the division is 14.75 and by using ceil() we get $maxPage = 15 Now that we know how many pages we have we make a loop to print the link. Each link will look something like this: We are almost complete. Just need to add a little code to create a 'Previous' and 'Next' link. With these links we can navigate to the previous and next page easily. And while we at it let's also create a 'First page' and 'Last page' link so we can jump straight to the first and last page when we want to.
<?php // creating previous and next link if ($pageNum > 1) if ($pageNum < $maxPage) // print the navigation link // and close the database connection // ... and we're done! Making these navigation link is actually easier than you may think. When we're on the fifth page we just make the 'Previous' link point to the fourth. The same principle also apply for the 'Next' link, we just need to add one to the page number. One thing to remember is that we don't need to print the 'Previous' and 'First Page' link when we're already on the first page. Same thing for the 'Next' and 'Last' link. If we do print them that would only confuse the one who click on it. Because we'll be giving them the exact same page.
|
We got a problem here...Take a look at this slightly modified version of paging.php. Instead of showing 20 numbers in a page, I decided to show just three. See the problem already? Those page numbers are running across the screen! Yuck! This call for a little modification to the code. Instead of printing the link to each and every page we will just saying something like "Viewing page 4 of 99 pages". Than means we havel remove these code : <?php $nav = ''; // ... the rest here And then modify this one <?php // print the navigation link // ...
Into this <?php // print the navigation link // ... |
No comments:
Post a Comment