Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

php - How to do a pagination from array?

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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...