PHP paging function code

不言
Release: 2023-03-24 06:58:01
Original
1747 people have browsed it

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;
Copy after login

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
PHP paging function code
Write PHP statement (index.php):

Copy code The code is as follows:

<?php 
// Page分页函数 
$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 == &#39;&#39;) $page = 1; 
if($page >= $page_count) $page = $page_count; 
$select_limit = $page_size; 
$select_from = ($page - 1) * $page_size.&#39;,&#39;; 
$pre_page = ($page == 1)? 1 : $page - 1; 
$next_page= ($page == $page_count)? $page_count : $page + 1 ; 
$pagenav .= "第 $page/$page_count 页 共 $rows 条记录 "; 
$pagenav .= "<a href=&#39;?page=1&#39;>首页</a> "; 
$pagenav .= "<a href=&#39;?page=$pre_page&#39;>前一页</a> "; 
$pagenav .= "<a href=&#39;?page=$next_page&#39;>后一页</a> "; 
$pagenav .= "<a href=&#39;?page=$page_count&#39;>末页</a>"; 
$pagenav.=" 跳到<select name=&#39;topage&#39; size=&#39;1&#39; onchange=&#39;window.location=\"?page=\"+this.value&#39;>\n"; 
for($i=1;$i<=$page_count;$i++){ 
if($i==$page) $pagenav.="<option value=&#39;$i&#39; selected>$i</option>\n"; 
else $pagenav.="<option value=&#39;$i&#39;>$i</option>\n"; 
} 
} // Page分页函数 
// 使用示例 
if (!$conn= mysql_connect("localhost", "root" ,"root")) die(&#39;数据库选择失败!&#39;); 
if (!mysql_select_db("test", $conn)) die(&#39;数据库选择失败!&#39;); 
mysql_query(&#39;set names GBK&#39;); 
// 用Page函数计算出 $select_from 从哪条记录开始检索、$pagenav 输出分页导航 
$rows = mysql_num_rows(mysql_query("select * from test")); 
Page($rows,2); 
$sql = "select * from test limit $select_from $select_limit"; 
$rst = mysql_query($sql); 
while ($row = mysql_fetch_array($rst)){ 
echo "$row[id] - $row[sex] - $row[name] <hr />"; 
} 
echo $pagenav; 
?>
Copy after login


Browse the index.php page, as shown in the figure:
PHP paging function code
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 code The code is as follows:

function getask(){ 
$sql = "select * from cms_ask where ansower <> &#39; &#39; "; 
//这里要改成方法 
$q_sq = mysql_query($sql); 
$count = mysql_num_rows($q_sq); 
$page_size = 8; 
$page_current = isset($GLOBALS[&#39;page&#39;]) ? intval($GLOBALS[&#39;page&#39;]) : 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 <> &#39; &#39; order by id desc limit " . (($page_current - 1) * $page_size) . "," . $page_size; 
$row=$this -> user -> getall("$sql"); 
foreach($row as $v){ 
echo &#39;<dl><dt>问:&#39;.$v["question"].&#39;</dt><dd>答:&#39;.$v["ansower"].date("Y-m-d H:i:s",$v["postTime"]).&#39;</dd></dl>&#39;; 

} 

$pagebar .= "<p class=\"page\">"; 
$pagebar .= "<ol>"; 
if ($page_current != 1) { 
$pagebar .= &#39;<li><a href="javascript:get_comment(1)" class="sx">FIRST</a></li>&#39;; 
} 
for ($i = $page_start; $i <= $page_end; $i++) { 
if ($i == $page_current) { 
$pagebar .= "<li><span class=\"sort\">" . $i . "</span></li>"; 
} else { 
$pagebar .= "<li><a href=&#39;javascript:get_comment(" . $i . ")&#39;>" . $i . "</a></li>"; 
} 
} 

if ($page_current != $page_count) { 
$pagebar .= "<li><a href=&#39;javascript:get_comment(" . $page_count . ")&#39; class=&#39;sx&#39;>END</a></li>"; 
} 
$pagebar .= "</ol>"; 
$pagebar .= " </p>"; 
echo $pagebar; 
}
Copy after login

##

The above is the detailed content of PHP paging function code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!