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