Record the paging method of PHP array data. Our data may not be stored in databases, and is often organized using arrays. Therefore, it is a common programming requirement to retrieve array data and paging. The paging principle is very simple. our data may not all be stored in the database, and most of the time it is organized using arrays. Therefore, it is a common programming requirement to retrieve array data and paging. The paging principle is very simple, that is, to obtain data in a certain range by the number of pages. PHP provides us with a very convenient function: array_slice (). The array_slice () function extracts a value from the array based on the conditions and returns it.
The following two paging examples are simple.
Public function books ($ pagenum = 1) {include_once ("page. php "); include (" arr_books.php "); foreach ($ data ['books '] as $ key => $ value) {$ name [$ key] = $ value ['name']; $ rank [$ key] = $ value ['rank '];} array_multisort ($ rank, SORT_NUMERIC, SORT_DESC, $ data ['books ']); // reverse Order // array_multisort ($ rank, SORT_NUMERIC, SORT_ASC, $ data ['books']); // order $ perpage = 10; $ count = count ($ data ['books ']); $ pages = new PageClass ($ count, $ perpage, $ pagenum, base_url (). 'Veda/books/{page}/'); $ start = ($ pagenum-1) * $ perpage; $ data ['bks '] = array_slice ($ data ['books'], $ start, $ perpage ); $ data ['Nav'] = $ pages-> myde_write ($ pagenum); $ this-> load-> view ('header', $ data ); $ this-> load-> view ('books ', $ data); $ this-> load-> view ('footer', $ data );}
Next:
$perpage = 2;$page = intval(getgpc('page')) ? intval($_G['gp_page']) : 1;$start = ($page - 1) * $perpage;$count = count($group_list);$list = array_slice($group_list, $start, $perpage);$multipage = multi($count, $perpage, $page, "home.php?mod=space&do=group&type=".getgpc('type'));
Array_slice () function
The array_slice () function extracts a value from the array based on the conditions and returns it. If the array has a string key, the returned array retains the key name.
Syntax: array_slice (array, offset, length, preserve)
Parameters |
Description |
Array |
Required. Specifies the input array. |
Offset |
Required. Value. Specifies the start position of the retrieved element. If it is a positive number, it is taken from the beginning to the end. if it is a negative value, it is taken from the back to the absolute value of the offset. |
Length |
Optional. Value. Specifies the length of the returned array. If it is a negative number, select the element of the absolute value from the back to the front. If this value is not set, all elements are returned. |
Preserve |
Optional. Possible values:
- True-reserved key
- False-default-reset key
|
Example 1:
"Dog",1=>"Cat",2=>"Horse",3=>"Bird");print_r(array_slice($a,1,2));// Array ( [0] => Cat [1] => Horse )?>
Example 2 (with a negative offset parameter ):
"Dog",1=>"Cat",2=>"Horse",3=>"Bird");print_r(array_slice($a,-2,1));// Array ( [0] => Horse )?>
Example 3 (set the preserve parameter to true ):
"Dog",1=>"Cat",2=>"Horse",3=>"Bird");print_r(array_slice($a,1,2,true));// Array ( [1] => Cat [2] => Horse )?>
Example 4 (with a string key ):
"Dog","b"=>"Cat","c"=>"Horse","d"=>"Bird");print_r(array_slice($a,1,2));// Array ( [b] => Cat [c] => Horse )?>
Bytes. Therefore, it is a common programming requirement to retrieve array data and paging. The paging principle is very simple...