php - How to do pagination from multiple array and calculate the total? -
i have array present data use display paganation.
$display_array = array (               [0] => array                   (                   [0] => 1                   [1] => 2                   [2] => 5                   [3] => 5                   )                [1] => array                   (                   [0] => 1                   [1] => 2                   [2] => 5                   [3] => 5                   )                [2] => array                   (                   [0] => 1                   [1] => 2                   )                [3] => array                   (                   [0] => 1                   [1] => 2                   )              ) i want paganation expected result this:
if defined $show_per_page = 2;
call paganation($display_array,1);//page 1 firstpage output :
1 2 call paganation($display_array,2);//next page 2 output :
    5     5   total:13 // total appear here    ....//next page n if defined $show_per_page = 3;
paganation($display_array,1);//page 1 firstpage output:
   1    2    5 paganation($display_array,2);//next page 2 output :
  5   total:13//now total appear here   1   2 paganation($display_array,3);//next page 3 output :
 5  5  total:10 // total appear here   1 if defined $show_per_page = 12; call paganation($display_array,1);//page 1 firstpage output :
1 2 5 5 total:13 // total here 1 2 5 5 total:13 // total here 1 2 total:3 //total 1 2 total:3 //total people here have idea?
something naive (because doesn't skip first few pages efficiently):
// array display // page show (1-indexed) // number of items show per page function pagination($display_array, $page, $show_per_page){     $start = $show_per_page * ($page-1);     $end   = $show_per_page * $page;     $i = 0;     foreach($display_array $section){         $total = 0;         foreach($section $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;         }     } } 
Comments
Post a Comment