<?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
8 9
9 10
10 11
total 1+2+3+..+11
*/
?>
有人可以帮我实现吗?
答案 0 :(得分:1)
以下是您的问题:$i
为负数,$show_per_page * ($page-1);
等于-6
因此,当您引用$arr[$i]
时,它没有显示任何内容,因为索引-6处没有任何内容,您可以尝试类似abs()的内容,例如:
for($i = $start; $i < $end; $i++){
echo ">>>".$arr[abs($i)]."<br>";
}
更新:
实际上这是:$_GET['page']
导致示例中索引的负值。
更新#2:
嗯,我顺便开始创建了这个,希望这能让你开始:
// 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);
}