Paging from array

Views: 8 Last modified: September 17th, 2011 Comments: 0

This snippet will help you out if you need paging from an array

<?php

// Your array
$arr = array('PHP','HTML','XHTML','JAVA','ASP','ASPX','AJAX');
$selected = array();

 // Per page
 $perpage = 2;

// Count your array values
$count = count($arr)-1;

// Set page
$current_page = (@$_GET['page']) ? (abs($_GET['page']) > ceil((count($arr) / $perpage)) || !is_numeric($_GET['page'])) ? 1 : abs($_GET['page']) : 1;

// Settings and calculations

$amount_pages = ceil($count / $perpage) + 1;
$show_values_b = ($current_page - 1) * $perpage;
$show_values_e = ($current_page - 1) * $perpage + $perpage;

// Loop through the array containing your values
for ($i = 0; $i <= $count; $i++)
{
    // Get values according to pages
    if ($i >= $show_values_b && $i < $show_values_e)
        $selected[] = $arr[$i];
}

// Show all the selected values
foreach ($selected as $show)
{
    echo $show.'<br />';
}

echo '<br />';

// Paging navigation
if ($current_page > 1)
echo '<a href="?page=' . ($current_page - 1) . '">&lt; Previous page</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
echo 'Page '. $current_page .' of '. $amount_pages;
if ($current_page < $amount_pages)
echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<a href="?page=' . ($current_page + 1) . '" class="right">Next page &gt;</a><br />';
?>
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {1+3} =  
    Anything to add ?

        You must be logged in to post a comment.