2

有一个$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
          )
      );

任何人都可以帮助我获得(output this "...." more)最重要的预期输出吗?

....告诉它还有更多元素需要显示下一页。或者它是上一页的剩余元素。

有要显示的09元素$A,所以

我设置

$show_per_page = 3;

输出(第一页):

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

输出(第二页):

....//output this "...." continue from first page 
  5
  2
Total:10
  3
.... //output this "...." more

输出(第三页):

 .... //output this "...." continue from  second page
    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

如果我设置

$show_per_page = 9;

输出:

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

目前我正在尝试使用该功能paging_from_multi_arr ,但我被困在如何实现获得加速结果的问题上:

// 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 $display_array array.
    // See http://php.net/manual/en/function.count.php
    if ($end < $total){
        echo "...";
    }
}


$show_per_page = 5;
paging_from_multi_arr($A,$_GET["page"]);

您对这里的功能有任何想法吗?或者可以给出更好的算法?

谢谢

4

2 回答 2

2

这应该给你你正在寻找的输出:

function flatten_display_array($display_array){
    $new_array = array();
    $count = 0;

    foreach($display_array as $main_order => $section){
        $total  = 0;
        foreach($section as $sub_order => $value){
            $new_array[] = array('main'  => $main_order,
                                 'sub'      => $sub_order,
                                 'value'    => $value);
            $total += $value;
            $count++;
        }
        // Add section's total to final element in section
        $new_array[$count-1]['total']   = $total;
    }

    return $new_array;
}

function paging_from_multi_array($display_array, $page = 1, $show_per_page = 3){
    if(isset($_GET['page']) && is_numeric($_GET['page'])){
        // Page number set externally
        $page = $_GET['page'];
    }
    if(isset($_GET['per_page']) && is_numeric($_GET['per_page'])){
        // Per page set externally
        $show_per_page  = $_GET['per_page'];
    }

    $start  = $show_per_page*($page-1);
    $end    = $show_per_page*$page;

    // Convert array to useable format
    $new_array = flatten_display_array($display_array);

    /* Formatting elements */
    $top_string = '....';   // Indicator continues is on previous page
    $bottom_string  = '....';   // Indicator continues on next page
    $br         = '<br />';     // Line break
    $indent     = '&nbsp;&nbsp;&nbsp;'; // Indent of value row

    $count  = 0;
    $string = '';

    for($i = $start; $i < $end; $i++){
        // Loop through visible range
        $string .= $indent.$new_array[$i]['value'].$br;
        if(isset($new_array[$i]['total'])){
            $string .= 'Total: '.$new_array[$i]['total'].$br;
        }
    }

    // Check previous page
    if($start > 0 && $start < count($new_array) && !isset($new_array[$start-1]['total'])){
        // Started mid-way through section
        $string = $top_string.$br.$string;
    }

    // Check next page
    if($end-1 < count($new_array) && !isset($new_array[$end-1]['total'])){
        // Stopped mid-way through section
        $string .= $bottom_string.$br;
    }

    return $string;
}

要使用它,只需调用paging_from_multi_array()函数:

echo paging_from_multi_array($A);

这样,如果未设置页码或每页显示的数量,它将默认为第一行中设置的那些paging_from_multi_array()(当前每页第 1 页和第 3 页)。

另外,查看下面的行/* Formatting Elements */以设置输出的元素(例如:'...'每个段的之前和之后。

于 2011-02-22T05:42:00.543 回答
1

没有“总”计算,应该是一个简单的分页(通过合并子数组并在大数组上分页)。但实际上,“总”与分页本身无关。所以我建议让我们先忘记分页的“总数”,然后将其插入适当的位置。

我的想法从创建这样的合并数组开始:

$B = array(
  0=>1,
  1=>2
  0=>3,
  1=>5,
  2=>2
  0=>3,
  1=>1,
  2=>6,
  3=>6
}

还有另一个数组来跟踪合并数组 $B 中 $A 的每个子数组的开头:

$C = {0, 2, 5}

然后我可以像往常一样简单地进行分页。关于“总数”,我们可以使用原始数组A来计算,然后根据C插入到合适的位置。

举个简单的例子,在第 2 页,max-per-page = 3 从 B,我得到子数组 B1:B(offset = 1, max-per-page=3, from B[3] to B[5 ] )

$B1 = {
  1=>5,
  2=>2,
  0=>3
}

基于 $C={0,2,5},通过一个简单的“for”循环,我们可以得到 $C[1] = 2 < 3 < 5 = 5 = $C[2] < length(B),所以在这里,我们知道我们将显示 2 个子数组(A[1] 和 a[2]);我们还必须计算并返回总计(A[1])。

这就是我的想法。我认为这会使事情更容易跟踪。

于 2011-02-22T02:27:56.837 回答