• 夺命雷公狗---Smarty NO:21 分页


    在smarty里面写一个分页

    demo6.php代码示例:

    <?php
    header(“Content-Type:text/html;charset=utf-8″);
    require “smarty/Smarty.class.php”;
    $smarty = new Smarty();
    //连接数据库
    mysql_connect(“localhost”,’root’,”);
    mysql_query(‘use xsphp’);
    mysql_query(‘set names utf8′);
    //读取所有记录,获取总数量
    $sql = ‘select count(*) as num from stu';//查询总条数
    $res = mysql_query($sql);
    $row = mysql_fetch_assoc($res);
    $count = $row[‘num’];  //拿到总条数
    //echo $count;
    //设置每页显示记录数
    $pagesize = 10;
    //获取总页数
    $pagecount = ceil($count/$pagesize);  //总条数除以每页多少条拿到总页数
    $page = isset($_GET[‘page’]) ? $_GET[‘page’] : 1;
    //设置上一页与下一页
    $pageprev = $page – 1;
    $pagenext = $page + 1;
    //判断是否越界
    //让他不能有负数
    if($pageprev == 0){
    $pageprev = 1;
    }
    //如果超过最大值让他等于总页数
    if($pagenext > $pagecount){
    $pagenext = $pagecount;
    }
    //小于1的都让他是1
    if($page < 1){
    $page = 1;
    }
    //超过总页数的让他去尾页
    if($page > $pagecount){
    $page = $pagecount;
    }
    //获取偏移量
    $offset = ($page -1)*$pagesize;
    //读取结果集分配到模版文件中
    $sql = “select id,name,age from stu order by id desc limit $offset,$pagesize”;
    $res = mysql_query($sql);
    $data = array();
    while($arr = mysql_fetch_assoc($res)){
    $data[] = $arr;
    }
    //通过assign方法分配变量到模版文件
    $smarty -> assign(‘data’,$data);//遍历出来的结果
    $smarty -> assign(‘pagecount’,$pagecount);//总页数
    $smarty -> assign(‘pageprev’,$pageprev);//总页数
    $smarty -> assign(‘pagenext’,$pagenext);//总页数
    $smarty -> assign(‘count’,$count);//总条数
    $smarty -> display(‘demo6.html’);

    demo6.html代码示例:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    <style type=”text/css”>
    table{
    width:400px;
    border:1px solid red;
    border-collapse:collapse;
    }
    table tr th, table tr td{
    border:1px solid red;
    text-align:center;
    }
    </style>
    </head>
    <body>
    <table>
    <tr>
    <th>编号</th>
    <th>姓名</th>
    <th>年龄</th>
    <th>操作</th>
    </tr>
    {foreach from=$data item=’row’}
    <tr>
    <td>{$row[‘id’]}</td>
    <td>{$row[‘name’]}</td>
    <td>{$row[‘age’]}</td>
    <td><input type=”checkbox” name=”id[]” value=”{$row[‘id’]}” /></td>
    </tr>
    {/foreach}
    <tr>
    <td colspan=”4″>
    单前共有{$count}条记录
    <a href=”demo6.php?page=1″>首页</a>
    <a href=”demo6.php?page={$pageprev}”>上一页</a>
    <a href=”demo6.php?page={$pagenext}”>下一页</a>
    <a href=”demo6.php?page={$pagecount}”>末页</a>
    <input type=”submit” name=”submit” value=”批量删除”/>
    </td>
    </tr>
    </table>
    </body>
    </html>
  • 相关阅读:
    苏教版国标本小学语文第一册汉字笔画
    C++ preprocessor __VA_ARGS__ number of arguments
    The Aggregate Magic Algorithms
    USB Mass Storage大容量存储的基本知识
    【转】驱动和应用层的三种通信方式
    Do You Actually Know What *P++ Does In C?
    Repeat Macro Cloak
    A SCSI command code quick reference
    USB device layout, descriptors, interface alternate setting
    RTOS Semaphore Services
  • 原文地址:https://www.cnblogs.com/leigood/p/5033444.html
Copyright © 2020-2023  润新知