mysarty實現分頁(三)之顯示數據

好了,前兩篇文章已經寫了兩個類了,現在我們要調用這兩個類,然後顯示出我們的數據庫裏的數據

先是我的配置smarty的文件:

init.inc.php

<?php
    require './libs/Smarty.class.php';
    require './page.class.php';
    require './mydb.php';
    $tpl = new Smarty();
    $tpl->template_dir ='./templates';
    $tpl->compile_dir ='./templates_c';
    $tpl->left_delimiter = '<{';
    $tpl->right_delimiter = '}>';

?>

然後是我的調用類的文件

index.php

<?php
    include 'init.inc.php';
    if(isset($_GET['page'])){
        $page=$_GET['page'];
    }else{
        $page=1;
    }
    if($page==0){$page=1;}
    $mydb = new Mydb('shop_car','car');
    $count=$mydb->getcount();
    $fpage = new page($count,$page,5);
    $pageinfo = $fpage->getpageinfo();
    $products = $mydb->getcontent($pageinfo['pagesize'],$pageinfo['offset']);
    if($products){
        $tpl->assign('title','商品列表');
        $tpl->assign('url','index.php');
        $tpl->assign('list',$products);
        $tpl->assign('pageinfo',$pageinfo);
    }else{
        echo "數據讀取失敗";
        exit;
    }
    $tpl->display('login.html');
?>

還有我的html頁面

login.html

<!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=UTF-8">
<title><{$title}></title>
</head>
<body>
<table border="1" align="center" width="500">
    <tr>
        <th colspan="4"><{$title}></th>
    </tr>
    <tr>
        <td>ID</td><td>商品名</td><td>所屬類別</td><td>單價</td>
    </tr>
    <{section name=list loop=$list}>
        <tr>
            <td><{$list[list].id}></td><td>
            <{$list[list].car_name}></td>
            <td><{$list[list].car_type}></td>
            <td><{$list[list].unit_price}>元</td>
        </tr>
    <{/section}>
</table>
<br></br>
<br></br>
<div align="right">
<{if $pageinfo.prev}>
<a href="<{$url}>?page=<{$pageinfo.prev}>">上頁</a>
<{/if}>
當前第<{$pageinfo.page}>頁
<{if $pageinfo.next}>
<a href="<{$url}>?page=<{$pageinfo.next}>">下頁</a>
<{/if}>
共<{$pageinfo.pagenum}>頁,<{$pageinfo.count}>個商品
</div>
</body>
</html>

好了,這樣再加上我先前的兩個類,這樣一個smarty的分頁功能就實現了!!!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章