建立数据库:
create database products,
CREATETABLEproduct(
namevarchar(50)NOTNULL,
pricedecimal(8,2)NOTNULLdefault0.00,
descriptionmediumtextNOTNULL,
PRIMARYKEY(productid)
)
编写mysql数据库类Mydb_class.php:
class Mydb{
private $mysqli;
function __construct(){
@$this->mysqli=new mysqli("localhost","root","fr1314520","products");
if (mysqli_connect_errno()) {
echo "数据库连接失败:".mysqli_connect_error();
return $this->mysqli=FALSE;
exit();
}
//$this->showError=$showError;
}
public function clase(){
if ($this->mysqli) {
$this->mysqli->close();
}
}
public function __destruct(){
$this->clase();
}
public function getrowtotal(){
$result=$this->mysqli->query("select *from product");
return $result->num_rows;
}
public function getpagerow($offset,$num){
$query="selectproductID,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;
}
}
}
编写php分页类:Mypage_class.php
class Mypage{
private $total;
private $current_page;
private $num;
private $offset;
private $pagenum;
function __construct($total,$currentpage=1,$num=5){
$this->total=$total;
$this->current_page=$currentpage;
$this->num=$num;
$this->offset=$this->getoffset();
$this->pagenum=$this->getpagenum();
}
private function getpagenum(){
return ceil($this->total/$this->num);
}
private function getoffset(){
return ($this->current_page-1)*$this->num;
}
private function getnextpage(){
if ($this->current_page==$this->getpagenum()) {
return false;
}else {
return $this->current_page+1;
}
}
private function getprevpage(){
if ($this->current_page==1) {
return false;
}else {
return $this->current_page-1;
}
}
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->current_page,
'row_offset'=>$this->offset,
'next_page'=>$this->getnextpage(),
'prev_page'=>$this->getprevpage(),
'page_start'=>$this->getstartnum(),
'page_end'=>$this->getendnum(),
);
return $pageinfo;
}
}
编写处理页:index.php
require "./libs/Smarty.class.php";
require './Mydb_class.php';
require './Mypage_class.php';
//require './page.php';
$smarty=new Smarty();
$smarty->template_dir="templates";
$smarty->compile_dir="templates_c";
$smarty->config_dir="config";
$smarty->cache_dir="cache";
$smarty->cache_lifetime=60*60;
$smarty->caching=0;
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
//$current_page=isset($_GET['page'])?intval($_GET['page']):1;//三元运算改为if判断便于理解
if ($_GET['page']){
$current_page=intval($_GET['page']);
}else {
$current_page=1;
}
if (!$smarty->is_cached("index.tpl",$current_page)) {
$mydb=new Mydb();
$total=$mydb->getrowtotal();
$fpage=new Mypage($total,$current_page,5);
$pageinfo=$fpage->getpageinfo();
$products=$mydb->getpagerow($pageinfo['row_offset'], $pageinfo['row_num']);
if ($products) {
$smarty->assign("tablename","商品列表");
$smarty->assign("url","index.php");
$smarty->assign("products",$products);
$smarty->assign("pageinfo",$pageinfo);
}else {
echo "数据库读取失败!";
exit();
}
}
$smarty->display("index.tpl", $current_page);
编写smarty模版:index.tpl
<html>
<head><title>Smarty实现商品列表 </title></head>
<body>
<{* 以表格的形式显式当前页的特定个数数据*}>
<tablealign="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=recordloop=$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><tdcolspan=4> 没有任何商品存在 </td></tr>
<{ /section }>
</table>
<{* 在下面段落中输出和分页有关的信息,并输出用户可以页面切换操作的链接*}>
<palign="center">
共<b> <{ $pageinfo.row_total }> </b>条记录,
显示第 <b> <{$pageinfo.page_start }> </b>-<b> <{ $pageinfo.page_end }> </b> 条记录
<ahref='<{ $url }>?page=1'>|<</a>
<{ if $pageinfo.prev_page }>
<ahref='<{ $url }>?page=<{ $pageinfo.prev_page }>'><<</a>
<{ else }>
<<
<{ /if }>
<{ if $pageinfo.next_page }>
<ahref='<{ $url }>?page=<{ $pageinfo.next_page }>'>>></a>
<{ else }>
>>
<{ /if }>
<ahref='<{ $url }>?page=<{ $pageinfo.page_num }>'>>|</a>
当前<b> <{ $pageinfo.current_page }>/<{ $pageinfo.page_num }> </b>页
</p>
</body>
</html>
运行结果:
总结:学习php是一条漫长而枯燥的道路:没有什么窍门,只有坚持和不断编程和练习,这个分页类,我花了好长时间才做出来,结合smarty做的,做这个小案例,我也有做的不好的地方,首先是花时间太长,不应该花这么长时间。然后是怕难,之前一提起oop就觉得是很难的东西的,有恐惧心理。再就是,没有找到窍门,现在我觉的做面向对象的开发是不断调试,每写一个属性和方法都应该实例化去调试以下,去理解为什么这么写,便于理解。加深自己的映像。回头把之前做的论坛实例贴上来。有时间再发博客。