开发者

Pagination algorithm from mutil-array?

Have an $A is the data for pagination:

   $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
          )
      );

I have a function

// 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;
        }
    }
}

Anybody could help me to implement the function paging_fro_multi_arr to get the expected ouput (output this "...." more) the most important ?

.... told that it is still more element need to display next page.

I set

$show_per_page = 3;

Output (for the first page):

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

Output (for the second page):

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

Output (for the third page):

1
6
6
Total:16

if I set

$show_per_page = 9;

OUTPUT:

        1
        2
      Total:3         
         3
         5
         2
      Tota开发者_运维问答l:10
         3
         1
         6
         6
     Total:16

if I set

$show_per_page = 5;

Output (for the first page):

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

Output (for the second page):

3
1
6
6
Total:16


That should be a starting point:

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";
    }
}


This should work:

// 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 "....";
    }
}


Why don't you add something like that to the very end of your function:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜