How to do a pagination from array?

2019-01-19 14:12发布

问题:

I have an array with the data which I want to display with 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 some thing like 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 to manipulate this?   
        // To get the result as I described below.
    }
}

I want do a pagination to get the expected result like this:

If I define $show_per_page = 2; then pagination($display_array, 1); outputs:

0602 xxx2
0602 xxxx3
Total:5

And paganation($display_array, 2); outputs:

0602 xxx3
0602 saa4
Total:7

If I define $show_per_page = 3;, then pagination($display_array, 1); outputs:

0602 xxx2
0602 xxxx3
Total: 5 
0602 xxx3

And paganation($display_array, 2); outputs:

0602 saa4
Total:7

If I define $show_per_page = 4; outputs:

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

回答1:

Have a look at this:

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

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 for $show_per_page = 3 is different than yours, but I'm not sure what you expect? You want to fetch everything that is left (i.e. '0602 saa4' and 7) plus one previous element (i.e. '0602 xxx3')?



回答2:

Use array_chunk:

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

or use a cleaner version of Marcin's Answer:

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


回答3:

$output = array_slice($inputArray,$page-1,$showPerPage); $output will contain the desired interval. $total = sizeof($inputArray); /the total items in the array/