php - How to do a pagination from array? -


i have array data want display pagination.

$display_array = array (     [0] => "0602 xxx2",     [1] => "0602 xxx3",     [2] => 5 // total= 2+3     [3] => "0602 xxx3",     [4] => "0602 saa4",     [5] => 7 // total = 3+4 ) 

i have try thing this

function pagination($display_array, $page) {        global $show_per_page;     $page = $page < 1 ? 1 : $page;     $start = ($page - 1) * $show_per_page;     $end = $page * $show_per_page;     for($i = $start; $i < $end; $i++)     {         ////echo $display_array[$i] . "<p>";         // how manipulate this?            // result described below.     } } 

i want pagination expected result this:

if define $show_per_page = 2; pagination($display_array, 1); outputs:

0602 xxx2 0602 xxxx3 total:5 

and paganation($display_array, 2); outputs:

0602 xxx3 0602 saa4 total:7 

if define $show_per_page = 3;, pagination($display_array, 1); outputs:

0602 xxx2 0602 xxxx3 total: 5  0602 xxx3 

and paganation($display_array, 2); outputs:

0602 saa4 total:7 

if define $show_per_page = 4; outputs:

0602 xxx2 0602 xxxx3 total:5 0602 xxx3 0602 saa4 total: 7  

have @ this:

    function paganation($display_array, $page) {         global $show_per_page;          $page = $page < 1 ? 1 : $page;          // start position in $display_array         // +1 account total values.         $start = ($page - 1) * ($show_per_page + 1);         $offset = $show_per_page + 1;          $outarray = array_slice($display_array, $start, $offset);          var_dump($outarray);     }      $show_per_page = 2;      paganation($display_array, 1);     paganation($display_array, 2);       $show_per_page = 3;     paganation($display_array, 1);     paganation($display_array, 2); 

the output is:

// when $show_per_page = 2; array   0 => string '0602 xxx2' (length=9)   1 => string '0602 xxx3' (length=9)   2 => int 5 array   0 => string '0602 xxx3' (length=9)   1 => string '0602 saa4' (length=9)   2 => int 7  // when $show_per_page = 3; array   0 => string '0602 xxx2' (length=9)   1 => string '0602 xxx3' (length=9)   2 => int 5   3 => string '0602 xxx3' (length=9) array   0 => string '0602 saa4' (length=9)   1 => int 7 

the output $show_per_page = 3 different yours, i'm not sure expect? want fetch left (i.e. '0602 saa4' , 7) plus 1 previous element (i.e. '0602 xxx3')?


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -