zoukankan      html  css  js  c++  java
  • PHP+MySQL Smarty简单分页显示示例

     

    一、分页程序的原理 

    分页程序有两个非常重要的参数:每页显示几条记录($pagesize)和当前是第几页($page)。

    有了这两个参数就可以很方便的写出分页程序,我们以MySql数据库作为数据源,在mysql里

    如果要想取出表内某段特定内容可以使用T-SQL语句:

    select * from table limit $offset,$pagesize来实现。这里的offset是记录偏移量,它的计算方法是

    $offset=$pagesize*($page-1),$pagesize是要每页显示的记录条数。

    也就是说select * from table limit 10,10这条语句的意思是取出表里从第11条记录往后的10条记录。

     

    二、主要代码解析

     

    (1)创建用例用表myTable 

     

    create table myTable(id int NOT NULL auto_increment,news_title 

     

    varchar(50),news_cont text,add_time datetime,PRIMARY KEY(id))

     

     

     

    (2)新建一个php命名为fenye.php

    <?php

    require ('../libs/Smarty.class.php');

    $smarty = new Smarty;
    //连接数据库
    $con = mysql_connect('localhost','root','');
    //数据连接的判断语句
    if(!$con){
    die('Could not connect!').mysql_error();
    }
    //选择'test'数据库(test自己随便建的数据库名称)
    mysql_select_db("test",$con);

    //获得记录总数
    $sql = "select count(*) from mytable";
    $rs = mysql_query($sql,$con);
    $myrow = mysql_fetch_array($rs);
    $numrow = $myrow[0];

    //设定每页的记录数
    $pagesize = 2;
    //计算总页数
    $pages = intval($numrow/$pagesize);
    //如果有余数pages再加1
    if($numrow%$pagesize){
    $pages++;
    }
    //设置当前页
    if(isset($_GET['page'])){
    $page = intval($_GET['page']);
    }else{
    $page = 1;
    }
    //判断page越界后的情况
    if($page>$pages){
    echo "<script language="JavaScript"> ";
    echo " alert("已经是尾页了"); ";
    echo " history.back(); ";
    echo "</script>";
    exit;
    }
    if($page<=0){
    echo "<script language="JavaScript"> ";
    echo " alert("已经是首页了"); ";
    echo " history.back(); ";
    echo "</script>";
    exit;
    }

    //计算记录偏移量
    $offset = $pagesize*($page-1);

    //读取指定记录数
    $result = mysql_query("select *from mytable order by id desc limit $offset,$pagesize",$con);
    while($row = mysql_fetch_array($result)){
    $title = $row['news_title'];
    $content = $row['news_cont'];
    $date = $row['add_time'];
    $arr[] = array("title"=>$title,"content"=>$content,"date"=>$date);

    }
    $smarty->assign("arr",$arr);
    $smarty->assign("page",$page);
    $smarty->assign("pages",$pages);
    $smarty->display("fenye.html");

    ?>

     

     

    (3)新建html命名为fenye.html (接收fenye.php数据)

     

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
    <title>Insert title here</title>
    </head>
    <body>
    <div align="center">
    <table border="0" width="80%">

    <tr>
    <td width="30%" bgcolor="#E0E0E0"><p align="center">标题</p></td>
    <td width="30%" bgcolor="#E0E0E0"><p align="center">内容</p></td>
    <td width="30%" bgcolor="#E0E0E0"><p align="center">发布时间</p></td>
    </tr>
    {foreach from=$arr item=it}
    <tr>
    <td width="30%">{$it.title}</td>
    <td width="30%">{$it.content}</td>
    <td width="30%">{$it.date}</td>
    </tr>
    {/foreach}
    </table>
    </div>

     

    <div align="center">
    {foreach from=$pages item=i}
    <a href="fenye3.php?page={$page-1}">上一页</a>
    共有{$pages}页({$page}/{$pages})
    <a href="fenye3.php?page={$page}"></a>
    <a href="fenye3.php?page={$page+1}">下一页</a>
    {/foreach}
    </div>
    </body>
    </html>

     

     

     

     

     

     

     

     

  • 相关阅读:
    JEECG与帆软报表集成
    各种数据库的锁表和解锁操作
    sql server数据库查询超时报错
    java项目部署后的文件路径获取
    js解决跨站点脚本编制问题
    java递归算法实现拼装树形JSON数据
    FreeMarker中的list集合前后台代码
    去除list集合中重复项的几种方法
    Java中Properties类的操作
    mysql 容灾 灾备 备份
  • 原文地址:https://www.cnblogs.com/xushu114/p/3208734.html
Copyright © 2011-2022 走看看