smarty分页

来源:互联网 发布:it域名注册 编辑:程序博客网 时间:2024/04/30 06:09

index.php

<?php
    include "./libs/Smarty.class.php";                 
    require("page_class.php");                      
    require("mydb_class.php");                    
    $tpl = new Smarty();                          
    $tpl->template_dir = "./templates/";              
    $tpl->compile_dir = "./templates_c/";             
    $tpl->cache_dir = "./cache/";                    
    $tpl->caching=false;                             
    $tpl->cache_lifetime=60*60*4;                
    $tpl->left_delimiter = '<{';                      
    $tpl->right_delimiter = '}>';                     

    $current_page=isset($_GET['page'])?intval($_GET['page']):1;

         
    if(!$tpl->is_cached("index.tpl", $current_page)) {
        $mydb=new MyDB();                             
        $total=$mydb->getRowTotal();                  
        $fpage=new Page($total,$current_page, 5);   
        $pageInfo=$fpage->getPageInfo();           
        $products=$mydb->getPageRows($pageInfo["row_offset"], $pageInfo["row_num"]);
        if($products) {                       
            $tpl->assign("tableName", "商品列表");
            $tpl->assign("url", "index.php");      
            $tpl->assign("products", $products);    
            $tpl->assign("pageInfo", $pageInfo);   
        }else {                                     
            echo "数据读取失败!";                   
            exit;                                  
        }
    }
    $tpl->display("index.tpl", $current_page);             
?>


mydb_class.php

<?php
    include "./libs/Smarty.class.php";                 
    require("page_class.php");                      
    require("mydb_class.php");                    
    $tpl = new Smarty();                          
    $tpl->template_dir = "./templates/";              
    $tpl->compile_dir = "./templates_c/";             
    $tpl->cache_dir = "./cache/";                    
    $tpl->caching=false;                             
    $tpl->cache_lifetime=60*60*4;                
    $tpl->left_delimiter = '<{';                      
    $tpl->right_delimiter = '}>';                     

    $current_page=isset($_GET['page'])?intval($_GET['page']):1;

         
    if(!$tpl->is_cached("index.tpl", $current_page)) {
        $mydb=new MyDB();                             
        $total=$mydb->getRowTotal();                  
        $fpage=new Page($total,$current_page, 5);   
        $pageInfo=$fpage->getPageInfo();           
        $products=$mydb->getPageRows($pageInfo["row_offset"], $pageInfo["row_num"]);
        if($products) {                       
            $tpl->assign("tableName", "商品列表");
            $tpl->assign("url", "index.php");      
            $tpl->assign("products", $products);    
            $tpl->assign("pageInfo", $pageInfo);   
        }else {                                     
            echo "数据读取失败!";                   
            exit;                                  
        }
    }
    $tpl->display("index.tpl", $current_page);             
?>



index.tpl


<html>
    <head><title> Smarty实现商品列表 </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
             <{* 以表格的形式显式当前页的特定个数数据 *}>
        <table align="center" border="1" width="90%">
            <caption><h1> <{ $tableName }> </h1></caption>
            <tr>
                <th>编号</th> <th>商品名称</th> <th>价格</th> <th>商品介绍</th>
            </tr>
                      <{* 使用section语句遍历从PHP中分配过来的商品数组$products *}>
            <{ section name=record loop=$products }>
                <tr>
                    <td> <{ $products[record].productID }> </td>
                    <td> <{ $products[record].name }> </td>
                    <td> <{ $products[record].price }> </td>
                    <td> <{ $products[record].description }> </td>
                </tr>    
            <{ sectionelse }>
                <tr><td colspan=4> 没有任何商品存在 </td></tr>
            <{ /section }>
        </table>
             <{* 在下面段落中输出和分页有关的信息,并输出用户可以页面切换操作的链接 *}>
        <p align="center">
        共<b> <{ $pageInfo.row_total }> </b>条记录,
        显示第 <b> <{ $pageInfo.page_start }> </b>-<b> <{ $pageInfo.page_end }> </b> 条记录
        <a href='<{ $url }>?page=1'>|&lt;</a>
        <{ if $pageInfo.prev_page }>
            <a href='<{ $url }>?page=<{ $pageInfo.prev_page }>'>&lt;&lt;</a>
        <{ else }>    
            &lt;&lt;
        <{ /if }>
        <{ if $pageInfo.next_page }>
            <a href='<{ $url }>?page=<{ $pageInfo.next_page }>'>>></a>
        <{ else }>    
            >>
        <{ /if }>
        <a href='<{ $url }>?page=<{ $pageInfo.page_num }>'>>|</a>
        当前<b> <{ $pageInfo.current_page }>/<{ $pageInfo.page_num }> </b>页
        </p>    
    </body>
</html>