PHP two-dimensional array paging-PHP source code

Source: Internet
Author: User
PHP two-dimensional array paging PHP two-dimensional array paging

Two ways: one is to use array_slice () directly, and the other is to control the number of for loops in the traditional mode. let's look at some suggestions, or who has a better way, I will share it with you. thank you.

Method 1:

  3, 'clickDate' =>'2010-10-11' ),array( 'clicks' => 2, 'clickDate' =>'2010-10-11' ),array( 'clicks' => 3, 'clickDate' =>'2010-10-09' ),array( 'clicks' => 1, 'clickDate' =>'2010-10-08' ),);$page = 2;$indexinpage=2;$newarr = array_slice($arr_click, ($page-1)*$indexinpage, $indexinpage);?>

The principle is as follows:
Same principle as paging
The array uses the array_slice (array, offset, length) function to retrieve a piece of value in the array according to the conditions.
Array: array to be processed
Offset: start position of the retrieved element
Length: returns the length of the array.
You can change the offset value on each page!
Method 2:
For example:

 3, 'clickdate' => '2017-10-11 '), array ('clicks' => 2, 'clickdate' => '2017-10-11 '), array ('clicks' => 3, 'clickdate' => '2017-10-09 '), array ('clicks' => 1, 'clickdate' => '2017-10-08 '),); $ size = 3; $ pnum = ceil (count ($ arr_click)/$ size ); if (isset ($ _ GET ['Page']) {$ page = intval ($ _ GET ['Page']); $ page = $ page> $ pnum? $ Pnum: $ page; $ page = $ page <$ pnum? 1: $ page;} else {$ page = 1;} for ($ I = 0; $ I <$ size; $ I ++) {if (! Isset ($ arr_click [($ page-1) * $ size + $ I]) break; echo'
'; Print_r ($ arr_click [($ page-1) * $ size + $ I]); echo'
';}?> 0? $ Page-1: 1;?> "> Previous Page"> Next page


The above is the content of PHP two-dimensional array paging processing. For more information, see PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.