I have an array that will be display with 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
)
)
for example:
If $show_per_page = 2 =>$nb_page = ceil(12 / 2 ) = 6
12 is the number of orders in array.
Html(table) output will loo开发者_高级运维k like this
firstpage
1.00
25.99
next page
5685.24
568.36
Total = 6 280,59
next page
1.00
25.99
next page
... etc ...
Anyboday Know to do this manipulate?
My Idea is to create a function ( I AM TRYING)
$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);
Here is simple class to achieve your pagination.
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);
精彩评论