多阵列的分页算法?

时间:2011-02-15 16:29:22

标签: php multidimensional-array pagination

$A是分页数据:

   $A = array(
      0=>array(
          0=>1,
          1=>2
          ),
      1=>array(
          0=>3,
          1=>5,
          2=>2
        ),
      2=>array(
          0=>3,
          1=>1,
          2=>6,
          3=>6
          )
      );

我有一个功能

// page to show (1-indexed)
// number of items to show per page
function paging_from_multi_arr($display_array, $page){
  Global $show_per_page;
    $start = $show_per_page * ($page-1);
    $end   = $show_per_page * $page;
    $i = 0;
    foreach($display_array as $main_order=>$section){
        $total = 0;
        foreach($section as $sub_order=>$value){
            if($i >= $end){
                break 2; // break out of both loops
            }

            $total += $value;
            if($i >= $start){
                echo $value.'<br>';
            }
            $i++;
        }
        if($i >= $start){
            echo 'Total:'.$total.'<br>';
        }
        if($i >= $end){
            break;
        }
    }
}

任何人都可以帮我实现函数paging_fro_multi_arr以使预期的输出(output this "...." more)最重要?

....告诉显示下一页还需要更多元素。

我设置

$show_per_page = 3;

输出(第一页):

  1 
  2
Total:3
  3
....//output this "...."  more 

输出(第二页):

  5
  2
Total:10
  3
.... //output this "...." more

输出(第三页):

1
6
6
Total:16

如果我设置

$show_per_page = 9;

输出:

        1
        2
      Total:3         
         3
         5
         2
      Total:10
         3
         1
         6
         6
     Total:16

如果我设置

$show_per_page = 5;

输出(第一页):

1
2
Total:3
3
5
2
Total:10
// .... //not output this "...." more now 

输出(第二页):

3
1
6
6
Total:16

3 个答案:

答案 0 :(得分:1)

这应该是一个起点:

function paging_from_multi_arr($page,$A){
    global $show_per_page;
    $start = ($page-1)*$show_per_page;
    $end   =  $page*$show_per_page;

    $flat_array = array();
    foreach ($A as $sub_array) {
        $flat_array = array_merge($flat_array, $sub_array);
    }

    $slice = array_slice($flat_array, $start, $show_per_page);

    foreach ($slice as $key => $value) {
        echo $value;
        echo "\n";
    }
}

答案 1 :(得分:1)

这应该有效:

// page to show (1-indexed)
// number of items to show per page
function paging_from_multi_arr($display_array, $page){
  Global $show_per_page;
    $start = $show_per_page * ($page-1);
    $end   = $show_per_page * $page;
    $i = 0;
    foreach($display_array as $main_order=>$section){
        $total = 0;
        foreach($section as $sub_order=>$value){
            if($i >= $end){
                break 2; // break out of both loops
            }

            $total += $value;
            if($i >= $start){
                echo $value.'<br>';
            }
            $i++;
        }
        if($i >= $start){
            echo 'Total:'.$total.'<br>';
        }
        if($i >= $end){
            break;
        }
    }
    $total = count($display_array, COUNT_RECURSIVE);
    // Total numbers of elements in your array.
    // See http://php.net/manual/en/function.count.php
    if ($end < $total){
        echo "....";
    }
}

答案 2 :(得分:0)

为什么不在功能的最后添加类似内容:

$total = 0;
foreach($display_array as $main_order => $section){
    $total += count($section);
}
if ($end < $total){
    echo "....";
}