2011-01-24 16 views
0

私はページングで表示される配列を持っています。例えばPHP配列はマルチ配列のすべてのインデックスに依存しますか?

Array 
    (
    [0] => Array 
     (
     [0] => 1.00 
     [1] => 25.99 
     [2] => 5685.24 
     [3] => 568.36 
    ) 

    [1] => Array 
     (
     [0] => 1.00 
     [1] => 25.99 
     [2] => 5685.24 
     [3] => 568.36 
    ) 

    [2] => Array 
     (
     [0] => 1.00 
     [1] => 25.99 
    ) 

    [3] => Array 
     (
     [0] => 1.00 
     [1] => 25.99 
    ) 

) 

If $show_per_page = 2 =>$nb_page = ceil(12/2) = 6 

12は、アレイ内の注文の数です。

HTML形式(テーブル)の出力は、この

FIRSTPAGE

1.00 
    25.99 

次のページ

 5685.24 
     568.36 
    Total = 6 280,59 

次のページ

1.00 
    25.99 

次のページ

のようになります。
... etc ... 

Anybodayこれを行うには分かりますか?

私の考えはここ

$show_per_page = 2; 
function paganation($base_arr,$page=1){ 
    GLOBAL $show_per_page; 

    foreach($base_arr as $idx=>$order){ 
     $total = 0; 
     for ($i=0;$i<$show_per_page;$i++){ 
      $total += $order[$i]; 
      echo $order[$i]."<p>"; 
     } 
     echo "==============<p>"; 
     echo "TOTAL: ".$total."<p>"; 
     echo "==============<p>"; 
    } 


} 


paganation($base_arr,1); 
+0

あなたはグローバル変数に現在のページの行数のカウントを保持する必要があるので、処理の任意のレベルからも同様にアクセス可能です。 –

+0

私はcode.thanksのサンプルスニペットを教えてください。 – kn3l

答えて

1

を(私がしようとしています)関数を作成することですが、あなたのページネーションを達成するための単純なクラスです。

class MyPaginator 
{ 
    protected $_array = null; 
    protected $_count = 0; 
    protected $show_per_page = 2; 

    public function __construct(array $array) 
    { 
     $this->setupItems($array); 
    } 

    private function setupItems(array $items) 
    { 
     foreach ($items as $key => $item) { 
      if (is_array($item)) { 
       $this->setupItems($item); 
      } else { 
       $this->_count++; 
       $this->_array[] = $item; 
      } 
     } 
    } 

    public function paganation($page=1) 
    { 
     $nextMaxItem = $this->show_per_page * $page; 
     $fromItem = ($page - 1) * $this->show_per_page; 
     $maxPages = (int) $this->_count/$this->show_per_page; 
     $total = 0; 

     for ($i = $fromItem; $i < $nextMaxItem; $i++) { 
      echo $this->_array[$i] . '<br />'; 
      $total += $this->_array[$i]; 
     } 
     echo "==============<p>"; 
     echo "TOTAL: " . $total . "<p>"; 
     echo "==============<p>"; 

     $previous = $page - 1; 
     if ($page > 1) { 
      echo '<a href="?page=' . $previous . '"><<</a>'; 
     } 

     $next = $page + 1; 
     if ($next <= $maxPages) { 
      echo '<a href="?page=' . $next . '">>></a>'; 
     } 
    } 
} 

$array = array(
    array(
     1.00, 25.99, 5685.24, 568.36 
    ), 
    array(
     1.00, 25.99, 5685.24, 568.36 
    ), array(
     1.00, 25.99 
    ), array(
     1.00, 25.99 
    ), 
); 
$page = isset($_GET['page']) ? $_GET['page'] : 1; 
$pag = new MyPaginator($array); 
$pag->paganation($page); 
関連する問題