This article introduces the PHP paging function code, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Prepare data:
New A 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, such as:
1 1 Xiaoqiang
2 0 Xiaohong
3 0 Xiaoli
4 1 Xiaobing
5 1 Zhang San
6 0 Li Si
7 0 Wu Xin
Write PHP statement (index.php):
Copy codeThe code is as follows:
= $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_count 页 共 $rows 条记录 "; $pagenav .= "首页 "; $pagenav .= "前一页 "; $pagenav .= "后一页 "; $pagenav .= "末页"; $pagenav.=" 跳到
Browse the index.php page, as shown in the figure:
It’s time to say byebye, it’s really useful!
PHP simple paging function
Write a PHP simple paging function, the database call is also written in it, the user can delete it by himself!
Copy codeThe code is as follows:
function getask(){ $sql = "select * from cms_ask where ansower <> ' ' "; //这里要改成方法 $q_sq = mysql_query($sql); $count = mysql_num_rows($q_sq); $page_size = 8; $page_current = isset($GLOBALS['page']) ? intval($GLOBALS['page']) : 1; $page_count = ceil($count / $page_size); $page_start = $page_current - 4; $page_end = $page_current + 4; if ($page_current < 5) { $page_start = 1; $page_end = 5; } if ($page_current > $page_count - 4) { $page_start = $page_count - 8; $page_end = $page_count; } if ($page_start < 1) $page_start = 1; if ($page_end > $page_count) $page_end = $page_count; $pagebar = ""; $sql = "select * from cms_ask where ansower <> ' ' order by id desc limit " . (($page_current - 1) * $page_size) . "," . $page_size; $row=$this -> user -> getall("$sql"); foreach($row as $v){ echo '
"; $pagebar .= "
##
The above is the detailed content of PHP paging function code. For more information, please follow other related articles on the PHP Chinese website!