array_slice function for array pagination


array_slice function we will use for array pagination to get some part of array

$items = array(1,2,3,4,5,6,7,8,9,10);
$limit = 3;

$qty_items = count($items);
$qty_pages = ceil($qty_items / $limit);

$curr_page = isset($_GET[‘page’]) ? $_GET[‘page’] : 1;
$next_page = $curr_page < $qty_pages ? $curr_page + 1 : null;
$prev_page = $curr_page > 1 ? $curr_page – 1 : null;

$offset = ($curr_page – 1) * $limit;
$items = array_slice($items, $offset, $limit);

$items // Array of elements

$offset // how many elements leave at the time of fetching

$limit //get number of elements from array

Pagination code

for($i = 1; $i <= $qty_pages; $i++){

echo $i;

}

ceil() function return integer number.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s