好了,前两篇文章已经写了两个类了,现在我们要调用这两个类,然后显示出我们的数据库里的数据
先是我的配置smarty的文件:
init.inc.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>