php开发分页实现代码第1/3页_php技巧

PHP中文网
Release: 2016-05-17 09:11:38
Original
1461 people have browsed it

php开发分页实现代码,学习php的朋友可以参考下

项目结构:

运行效果:

conn.php


<?php 
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误"); 
mysql_select_db("form", $conn); 
mysql_query("set names &#39;GBK&#39;"); //使用GBK中文编码; 
//替换空格,回车键 
function htmtocode($content) { 
$content = str_replace("\n", "<br>", str_replace(" ", " ", $content)); 
return $content; 
} 
?>
Copy after login


page.php


<?php 
 
function _PAGEFT($totle, $displaypg = 20, $url = &#39;&#39;) { 

global $page, $firstcount, $pagenav, $_SERVER; 

$GLOBALS["displaypg"] = $displaypg; 

 if (!$page) 
$page = 1; 
if (!$url) { 
$url = $_SERVER["REQUEST_URI"]; 
} 
//URL分析: 
$parse_url = parse_url($url); 
$url_query = $parse_url["query"]; //单独取出URL的查询字串 
if ($url_query) { 
$url_query = ereg_replace("(^|&)page=$page", "", $url_query); 
$url = str_replace($parse_url["query"], $url_query, $url); 
if ($url_query) 
$url .= "&page"; 
else 
$url .= "page"; 
} else { 
$url .= "?page"; 
} 
$lastpg = ceil($totle / $displaypg); //最后页,也是总页数 
$page = min($lastpg, $page); 
$prepg = $page -1; //上一页 
$nextpg = ($page == $lastpg ? 0 : $page +1); //下一页 
$firstcount = ($page -1) * $displaypg; 
//开始分页导航条代码: 
$pagenav = "显示第 <B>" . ($totle ? ($firstcount +1) : 0) . "</B>-<B>" . min($firstcount + $displaypg, $totle) . "</B> 条记录,共 $totle 条记录"; 
//如果只有一页则跳出函数: 
if ($lastpg <= 1) 
return false; 
$pagenav .= " <a href=&#39;$url=1&#39;>首页</a> "; 
if ($prepg) 
$pagenav .= " <a href=&#39;$url=$prepg&#39;>前页</a> "; 
else 
$pagenav .= " 前页 "; 
if ($nextpg) 
$pagenav .= " <a href=&#39;$url=$nextpg&#39;>后页</a> "; 
else 
$pagenav .= " 后页 "; 
$pagenav .= " <a href=&#39;$url=$lastpg&#39;>尾页</a> "; 
//下拉跳转列表,循环列出所有页码: 
$pagenav .= " 到第 <select name=&#39;topage&#39; size=&#39;1&#39; onchange=&#39;window.location=\"$url=\"+this.value&#39;>\n"; 
for ($i = 1; $i <= $lastpg; $i++) { 
if ($i == $page) 
$pagenav .= "<option value=&#39;$i&#39; selected>$i</option>\n"; 
else 
$pagenav .= "<option value=&#39;$i&#39;>$i</option>\n"; 
} 
$pagenav .= "</select> 页,共 $lastpg 页"; 
} 
include("conn.php"); 
$result=mysql_query("SELECT * FROM `test`"); 
$total=mysql_num_rows($result); 
//调用pageft(),每页显示10条信息(使用默认的20时,可以省略此参数),使用本页URL(默认,所以省略掉)。 
_PAGEFT($total,5); 
echo $pagenav; 
$result=mysql_query("SELECT * FROM `test` limit $firstcount,$displaypg "); 
while($row=mysql_fetch_array($result)){ 
echo "<hr><b>".$row[name]." | ".$row[sex]; 
} 
?>
Copy after login


list.php
[code]
include("conn.php");
$pagesize=5;
$url=$_SERVER["REQUEST_URI"];
$url=parse_url($url);
$url=$url[path];
$numq=mysql_query("SELECT * FROM `test`");
$num = mysql_num_rows($numq);
if($_GET


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