PHP Pagination
Pagination of pages in PHP is a topic famous
topic. So here I not explain any new php codes for Php Pagination. I will explain how you can use pagination for data
held within an array.
Normally you did only need to paginate your data that
if you’ve got quite a lot of it in.With database systems of course pagination (Php Pagination) can be achieved
relatively easily by specifying the offset parameters in an SQL query. But what
if your data didn’t come from a database table and instead came from a flat
file. So look at the following code which expalin exactly how it’s done Php Pagination.
//
Data, normally from a flat file or some other source
$data
= "Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|Item 7|Item 8|Item 9|Item
10";
//
Put our data into an array
$dataArray
= explode('|', $data);
//
Get the current page
$currentPage
= trim($_REQUEST[page]);
//
Pagination settings
$perPage
= 3;
$numPages
= ceil(count($dataArray) / $perPage);
if(!$currentPage
|| $currentPage > $numPages)
$currentPage
= 0;
$start
= $currentPage * $perPage;
$end
= ($currentPage * $perPage) + $perPage;
//
Extract ones we need
foreach($dataArray
AS $key => $val)
{
if($key
>= $start && $key < $end)
$pagedData[] = $dataArray[$key];
}
Created the $data
array which contains a long line of items distribute up by the pipe '|'
character. Realistically this would be real data just for this tutorial I'll
keep it simple. Then, using the explode()
function I've cut up the $data
variable into array using '|' as the delimeter.
The line 8 in Php Pagination simply gets the current page number one is provided.
Lines 7 to 13 Php Pagination do the math calculations which make this array
pagination work. Now set how many items we'd like to display per page into the
variable, $perPage. In the example
above I've set this to 3.
On line 8 we're working out how many pages there
are going to be. This can be done by dividing the total number of items (by
using the count() function) by the
items per page value. Notice on this line that I'm also using the ceil() function. This basically rounds
the number up (e.g. 5.134 becomes 6).
We then have a simply if statement on lines 9 and
10 of Php Pagination. It's basically
saying that if no page number has been provided or if the provided page number
is more than the number of pages, set it to 0. This stops people from trying to
access pages which have no items.
On lines 14 and 15 of Php Pagination we're setting the $start and $end variable
which you might recognize if you've done pagination using SQL queries before.
The $start variable holds the lowest ID of the item which can be displayed on
this page. The $end variable is maximum cap which the items ID can be to be
displayed (actually, it's one above, but this depends on how you do your if
statement on line 17 of Php Pagination).
Great, we're nearly there. Now, on line 15 of Php Pagination we start a foreach statement which loops through
each of our data items. Inside this loop is a simple if statement to see if the
id of the current data item is above (or equal to) the $start value and below the $end
value. If it is then we place a copy of it into our $pagedData array.
Once the foreach
loop has finished the $pagedData
array now contains all of the data items which we should be displaying on the
current page. All we have to do now is to loop through and display them. This
has been shown in the following code snippet.
foreach($pagedData
AS $item)
echo $item . "<br>";
As displaying the data goes that's it. Now need to display
the pagination links to let you navigate your way through the pages. Here's the
code for Php Pagination.
if($currentPage > 0 && $currentPage < $numPages)
echo '<a
href="?page=%27%20.%20%28$currentPage%20-%201%29%20.%20%27">«
Previous page</a><br>';
if($numPages > $currentPage && ($currentPage + 1) <
$numPages)
echo '<a
href="?page=%27%20.%20%28$currentPage%20+%201%29%20.%20%27"
class="right">Next page »</a><br>';
Above codes of Php Pagination contain two quite simple if statements. First is
displaying the "previous page" link and the another is for displaying
the "next page" link.
If first statement checking to see if the current
page is more than 0 and if the current page is less than the total number of
pages.
The second if statement is checking to see if the
total number of pages is more than the current page number and if there are any
more pages after the current page.
Atleast you have done Php Pagination.
Give us such a impressive blog and article on blog on the internet. Keep writing on the internet.
ReplyDeletehttp://ilearn24x7.com/php-training.php