<?php
$B = array(
0=>1,
1=>2,
2=>3,
3=>4,
4=>5,
5=>6,
6=>7,
7=>8,
8=>9,
9=>10,
10=>11
);
function pagination_from_array($arr, $show_per_page, $page=1){
$start = $show_per_page * ($page-1);
$end = $show_per_page * $page;
for($i = $start; $i < $end; $i++){
echo ">>>".$arr[$i]."<br>";
}
if($end-1 < count($arr)) {
echo '............';
}
}
pagination_from_array($B , 6, $_GET['page']);
/*
//Dislay in html table
//=> page1
key | value
0 1
1 2
2 3
3 4
4 5
5 6
........
//=> page 2
key | value
6 7
7 8
开发者_StackOverflow中文版 8 9
9 10
10 11
total 1+2+3+..+11
*/
?>
Could anyone help me to implement this?
Here is your problem: $i
is a negative number as $show_per_page * ($page-1);
equals -6
So when your referencing $arr[$i]
it's not displaying anything because there is nothing at index -6, You could try something like the abs(), Example:
for($i = $start; $i < $end; $i++){
echo ">>>".$arr[abs($i)]."<br>";
}
UPDATE:
Well it's actually this: $_GET['page']
that's causing the negative value for the index in your example.
UPDATE #2:
Well I went along and quickly created this, hope this gets you started:
// Page Count
$page_count = 100;
// Build the array
for($p = 1; $p <= $page_count; $p++) {
$pages[] = $p;
}
// Print the array for testing
//echo print_r($pages, true)."\n";
function pagination_from_array($arr, $show_per_page, $page=1){
$total_pages = count($arr);
$paginate_total_pages = $total_pages / $show_per_page;
$start = $show_per_page * ($page-1);
$end = $show_per_page * $page;
//echo "Start: ".$start."\n";
//echo "End: ".$end."\n";
//echo "Total: ".$total_pages."\n";
//echo "Pageinate: ".$paginate_total_pages."\n";
//echo "Page: ".$page."\n";
if(($paginate_total_pages) + 1 < $page) {
return; // no pages to display
}
if($total_pages < $start) {
return; // no pages to display
}
for($i = $start; $i < $end; $i++){
if(array_key_exists(abs($i),$arr)) {
echo ">>>".$arr[abs($i)]."<br />\n";
}
}
if($end-1 < count($arr)) {
echo "............<br />\n";
}
}
$display_pages = 6;
$pages_to_display = (count($pages) / $display_pages) + 1;
echo "Pages to display: ".$pages_to_display."\n";
for($d = 1; $d <= $pages_to_display; $d++) {
pagination_from_array($pages,$display_pages, $d);
sleep(1);
}
精彩评论