Php分页取决于多数组的每个索引?

时间:2011-01-24 15:13:30

标签: php multidimensional-array pagination

我有一个将以分页显示的数组。

  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(表格)输出将如下所示

第一页

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

1 个答案:

答案 0 :(得分: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);