Home  >  Article  >  Backend Development  >  PHP paging function code (simple and practical)_PHP tutorial

PHP paging function code (simple and practical)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:33:231394browse

Prepare data:

Create a new database test
Execute the following statement (create a new table test: three fields of id, sex and name)

CREATE TABLE `test` (
`id` INT( 4 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`sex` INT( 1 ) NOT NULL ,
`name` VARCHAR( 20 ) NOT NULL ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_bin;

Add data to the test table, for example:
1 1 Xiaoqiang
2 0 Xiaohong
3 0 Xiaoli
4 1 Xiaobing
5 1 Zhang San
6 0 Li Si
7 0 Wu Xin
PHP paging function code (simple and practical)_PHP tutorial
Write PHP statement (index.php):

Copy code The code is as follows:

// Page paging function
$page = $_GET["page"];
function Page($rows,$page_size){
global $page,$select_from,$select_limit,$pagenav;
$page_count = ceil($rows/$page_size);
if($page <= 1 || $page == '') $ page = 1;
if($page >= $page_count) $page = $page_count;
$select_limit = $page_size;
$select_from = ($page - 1) * $page_size.', ';
$pre_page = ($page == 1)? 1 : $page - 1;
$next_page= ($page == $page_count)? $page_count : $page + 1 ;
$ pagenav .= "Page $page/$page_count total $rows records ";
$pagenav .= "Homepage ";
$pagenav .= "Previous page ";
$pagenav .= " ";
$pagenav .= "
Last page";
$pagenav.=" Jump to