smarty商品分页
来源:互联网 发布:matlab遗传算法代码 编辑:程序博客网 时间:2024/06/11 17:14
index.tpl 模板,用于显示分页效果
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><head><title>smarty 分页</title></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 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>条记录 <a href='<{ $url }>?page=1'>|<</a> <{if $pageInfo.prev_page }> <a href='<{ $url }>?page=<{ $pageInfo.prev_page }>'><<</a> <{ else }> << <{ /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>
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=1;
$tpl->cache_lifetime=60*60;
$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();
//echo $pageInfo["row_offset"]."<br>";
//echo $pageInfo["row_num"]."<br>";
$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
class MyDB{
private $mysqli; //保存mysli扩展中的mysqli对象
public function __construct(){
$this->mysqli=new MySQLi("localhost","root","","sqlitest"); //连接数据库
$this->mysqli->query("set names utf8;"); //修改数据库编码为UTF8
if(mysqli_connect_errno()){ //连接错误,显示错误信息并退出程序
echo "连接失败,原因是:".mysqli_connect_error();
$this->mysqli=FALSE;
exit();
}
@$this->showError=$showError; //为成员属性showError赋值
}
public function __destruct(){
$this->close();
}
public function close(){ //调用方法关闭数据库 释放资源
if($this->mysqli)
$this->mysqli->close();
$this->mysqli=FALSE;
}
public function getRowTotal(){ //调用方法返回商品中记录总数
$result=$this->mysqli->query("select * from product");
return @$result->num_rows;
}
public function getPageRows($offset,$num){ //获取指定的一段数据 由$offset和$num 控制
$query="select productID,name,price,description from product order by productID limit $offset,$num;";
if($result=$this->mysqli->query($query)){
while($row=$result->fetch_assoc()){
$allProduct[]=$row;
}
$result->close();
return $allProduct;
}else{
return FALSE;
}
}
}
?>
Page_class.php 分页模板
<?php
class Page{ //分页类
private $total; //保存所有的数据表记录的总条数
private $page; //保存当前第几页
private $num; //设置每页显示记录的页数
private $pageNum; //保存一共被分为多少页的数
private $offset; //保存从数据库中取记录的开始偏移数
function __construct($total,$page=1,$num=5){
$this->total=$total;
$this->page=$page;
$this->num=$num;
$this->pageNum=$this->getPageNum();
$this->offset=$this->getOffset();
}
private function getPageNum(){ //返回计算后的页面总数
return ceil($this->total/$this->num);
}
private function getNextPage(){ //调用方法返回下一面的索引
if($this->page==$this->pageNum)
return false;
else
return $this->page+1;
}
private function getPrevPage(){ //调用方法返回上一面的索引
if($this->page==1)
return false;
else
return $this->page-1;
}
private function getOffset(){ //调用方法返回数据库查询所需要的偏移量
return ($this->page-1)*$this->num;
}
private function getStartNum(){ //返回当前页结束的偏移数
if($this->total==0)
return 0;
else
return $this->offset+1;
}
private function getEndNum(){ //返回当前页结束的偏移数
return min($this->offset+$this->num,$this->total);
}
/* 将所有和当前页面有关的值放入一个数组返回 */
public function getPageInfo(){
$pageInfo=array(
"row_total"=>$this->total,
"row_num"=>$this->num,
"page_num"=>$this->getPageNum(),
"current_page"=>$this->page,
"row_offset"=>$this->getOffset(),
"next_page"=>$this->getNextPage(),
"prev_page"=>$this->getPrevPage(),
"page_start"=>$this->getStartNum(),
"page_end"=>$this->getEndNum()
);
return $pageInfo;
}
}
?>
下载:
带登陆:http://download.csdn.net/detail/yuluo727282752/3795042
不带登陆:http://download.csdn.net/detail/yuluo727282752/3794564
- smarty商品分页
- smarty分页
- smarty分页
- smarty分页
- smarty分页
- Smarty的分页实现
- Smarty的分页实现
- 分页函数(Smarty + Adodb)
- Smarty的分页实现
- smarty的简单分页
- SMARTY分页详解
- smarty的简单分页
- SMARTY分页详解
- smarty的简单分页
- SMARTY分页插件
- 应用smarty分页
- smarty实现分页
- smarty实现分页代码
- Eclipse的性能分析差距TPTP
- Linux Performance Monitoring and Tuning Introduction
- 我自己的vim编辑器-我的插件集合
- HTML5大势所趋 Adobe放弃移动Flash 微软将停止发展Silverlight
- 怎么打蝴蝶结
- smarty商品分页
- nand flash结构以及读写分析——写的比较经典
- vs2010中的ipch文件夹和sdf文件
- Linux启动过程简介
- [模式之集思广义] Adapter Pattern
- Flash Builder显示swf文件
- 职场不能说的秘密
- shell取日期串
- c/c++内存操作函数 是否初始化