2011-01-25 12 views
9

Ho una matrice con i dati che voglio visualizzare con l'impaginazione.Come eseguire una paginazione dall'array?

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

devo provare qualche cosa come questa

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 to manipulate this? 
     // To get the result as I described below. 
    } 
} 

voglio fare un'impaginazione per ottenere il risultato atteso in questo modo:

Se io definisco $show_per_page = 2; poi pagination($display_array, 1); uscite:

0602 xxx2 
0602 xxxx3 
Total:5 

E paganation($display_array, 2); uscite:

0602 xxx3 
0602 saa4 
Total:7 

Se definisco $show_per_page = 3;, quindi pagination($display_array, 1); uscite:

0602 xxx2 
0602 xxxx3 
Total: 5 
0602 xxx3 

E paganation($display_array, 2); uscite:

0602 saa4 
Total:7 

Se definisco $show_per_page = 4; uscite:

0602 xxx2 
0602 xxxx3 
Total:5 
0602 xxx3 
0602 saa4 
Total: 7 
+1

Allora, qual è la tua domanda? O stai solo cercando qualcuno che faccia il tuo lavoro? –

+0

Puoi descrivere cosa vuoi manipolare? – Gajahlemu

+0

$ end = $ start + $ show_per_page; certamente? –

risposta

15

Date un'occhiata a questo:

function paganation($display_array, $page) { 
     global $show_per_page; 

     $page = $page < 1 ? 1 : $page; 

     // start position in the $display_array 
     // +1 is to account for 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); 

L'output è:

// 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 

L'uscita a $ show_per_page = 3 è diversa dalla vostra, ma non sono sicuro di cosa ti aspetti? Vuoi recuperare tutto ciò che è rimasto (ad esempio '0602 saa4' e 7) più un elemento precedente (ad esempio '0602 xxx3')?

+0

per altro caso ok, ma ho un test, $ show_per_page = 4 Non ho ottenuto il totale. – kn3l

+0

e $ show_per_page = 1 Ho ottenuto due display di entris, infatti dovrebbe contenere solo una voce. – kn3l

+0

E perché tu $ offset = $ show_per_page + 1; Perché non è sempre così .Marcin – kn3l

0

$ output = array_slice ($ inputArray, $ page-1, $ showPerPage); L'uscita $ conterrà l'intervallo desiderato. $ totale = sizeof ($ inputArray);/le voci totali nella matrice/

+1

perché total = sizeof ($ inputArray) ;? – kn3l

4

Uso array_chunk:

function paginate($array, $pageSize, $page = 1) 
{ 
    $pages = array_chunk($array, $pageSize); 
    return $page > sizeof($pages) ? [] : $pages[$page - 1]; 
} 

o utilizzare una versione più pulita della risposta di Marcin:

function paginate($array, $pageSize, $page = 1) 
{ 
    $page = $page < 1 ? 1 : $page; 
    $start = ($page - 1) * $pageSize; 
    return array_slice($array, $start, $pageSize); 
} 
+0

l'idea array_chunk è molto carina, grazie per questo. . . ora per costruire un sistema di menu basato su questo. :) –

+1

@MichaelTunnell sei il benvenuto. – PHPst