php分页多种实现程序代码
本文章总结了目前常用的几种分页代码,比如说上一页,下一页之类的,还有一种数字分页,像1,2,3...之类的等.
分页代码如下:
- <?php
-
- $page=isset($_GET['page'])?intval($_GET['page']):1;
- $num=5;
- $conn = mysql_connect('127.0.0.1','root','');
- if (!$conn) {
- die('Could not connect:'.mysql_error());
- }
- mysql_select_db('shop');
-
- $total=mysql_num_rows(mysql_query("select * from user"));
-
- $pagenum=ceil($total/$num);
-
- If($page>$pagenum || $page == 0){
- Echo 'Error : Can Not Found The page.';
- Exit;
- }
-
-
- $offset=($page-1)*$num;
- $result=mysql_query("select * from user limit $offset,$num");
- while($it=mysql_fetch_array($result)){
- echo 'id:'.$it['id'].' name:'.$it['name'].'<br />';
- }
-
- for($i=1;$i<=$pagenum;$i++){
- $show=($i!=$page)?"<a href='index.php?page=".$i."'>$i</a>":"<b>$i</b>";
- echo $show." ";
- }
- echo $total.'条记录,每页5条,共'.$pagenum.'页';
- mysql_free_result($result);
- mysql_close($conn);
- ?>
实例2,代码如下:
- <?
-
- function php_page($page_dbname,$page_size,$page)
- {
- if ($page=="")
- {$page=1;};
- if ($ljjl=="")
- {$ljjl=0;};
- if($page)
- {
-
-
- $query="select count(*) as total from ".$page_dbname;
- $result=mysql_query($query);
- $message_count=mysql_result($result,0,"total");
- $page_count=ceil($message_count/$page_size);
- $offset=($page-1)*$page_size;
- $query="select * from ".$page_dbname." order by id desc limit $offset, $page_size";
- $result=mysql_query($query);
-
- while ($myrow=@mysql_fetch_array($result))
- {
- echo $myrow[name]." | ".$myrow[number]." | ".$myrow[tel]." | ".$myrow[address]."<br><br>";
- }
-
- }
- echo "页次:".$page."/";
- echo $page_count."页 记录:";
- echo $message_count."条"."<br>";
-
- if($page!=1)
- {
- echo "<a href=index.php?page=1>首页</a> ";
- echo "<a href=index.php?page=".($page-1).">上一页</a> ";
- }
- else
- {
- echo "首页 ";
- echo "上一页 ";
- }
-
- for($i=1; $i<=$page_count; $i++)
- {
- if ($page==$i)
- {echo $i." ";}
- else
- {echo "<a href=index.php?page=$i>$i</a> ";}
- }
-
- if($page<$page_count)
- {
- echo "<a href=index.php?page=".($page+1).">下一页</a> ";
- echo "<a href=index.php?page=".$page_count.">尾页</a>";
- }
- else
- {
- echo "下一页 ";
- echo "尾页";
- }
- }
- ?>
conn.php 连接数据库,代码如下:
- <?php
- $id=mysql_connect("localhost","root","root")or dir('连接失败:' . mysql_error());
-
-
- if(mysql_select_db("db_database06",$id))
- echo "";
- else
- echo ('连接失败:' . mysql_error());
- mysql_query("set names gb2312");
- ?>
主程序,将上面俩个文件包含进来,再调用一个php_page()就可完成分页,代码如下:
- <?php
- require_once("conn.php");
- require_once("function.php");
- php_page("tb_insert",3,$_GET[page]);
- ?>
上面的一种是文字分页,比如说上一页,下一页之类的,还有一种数字分页,像1,2,3...之类的,这个比较简单,前者稍微复杂些,最后一种是他们2者结合,代码如下:
- --> 1
- if(isset($_GET['page'])){
- $page = intval($_GET['page']);
- }
- else {
- $page=1;
- }
- $PageSize = 1;
-
- $sql = "select count(*) from blog";
- $result = mysql_query($sql);
- $row = mysql_fetch_row($result);
- $amount = $row[0];
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $PageCount = ceil($amount/$PageSize);
- if($Page>$PageCount|$page==0){
- echo "不能发现此页!";
- exit();
- }
-
-
- $PageOut = '';
- if($page==1){
- $PageOut .= '第一页|上一页';
- }
- else{
- $PageOut .= '<a href="index.php?page=1">第一页& lt;/a>|<a href="index.php?page='.($page-1).'">上一页</a>|';
- }
- if($page==$PageCount||$PageCount==0){
- $PageOut .= '下一页|尾页';
- }
- else{
- $PageOut .= '<a href="index.php?page='.($page+1).'">下一页</a>|<a href="index.php?page='.$PageCount.'">尾页</a>';
- }
-
- if($amount){
- $sql="select * from blog limit ".($page-1)*($PageSize).",$PageSize";
- $result=mysql_query($sql);
- while($row=mysql_fetch_array($result)){
- $blogs[] = array('bid'=>$row['bid'],'title'=>$row['title']);
- foreach ($blogs as $blog){
- $title=$blog['title'];
- }
- $output = "<a href=index.php?action=blog_del&bid=$blog[bid]>删除</a>".
- "<a href=index.php?action=blog_edit&bid=$blog[bid]>编辑</a>".
- "<a href=index.php?action=blog_view&bid=$blog[bid]>查看</a>";
- include("template/default/blog.tpl.php");
- echo $PageOut;
- }
- for($i=1;$i<=$PageCount;$i++){
- $Pageshow = ($i!=$page)?"<a href='index.php?page=".$i."'>$i</a>":"<b>$i</b>";
- echo $PageShow;
- }
- echo $amount.'条记录,每页'.$PageSize.'条,共'.$PageCount.'页';
- }