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 

出力 (2 ページ目):

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

出力 (3 ページ目):

 .... //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 

出力 (2 ページ目):

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]) を表示することがわかっています。また、total(A[1]) も計算して返す必要があります。

それが私の考えです。物事を追跡しやすくなると思います。

于 2011-02-22T02:27:56.837 に答える