一 代码
1、编写入口文件
<?php
define('THINK_PATH','../ThinkPHP'); //定义ThinkPHP框架路径(相对于入口文件)
define('APP_NAME','App'); //定义项目名称
define('APP_PATH','./App'); //定义项目路径
require(THINK_PATH.'/ThinkPHP.php'); //加载框架入口文件
App::run(); //实例化一个网站应用实例
?>
2、编写配置文件
<?php
return array(
'APP_DEBUG'=>false,//关闭调试模式
'DB_TYPE'=>'mysql',//数据库类型
'DB_HOST'=>'localhost',//数据库主机名
'DB_USER'=>'root',//数据库用户名
'DB_PWD'=>'root',//数据库密码
'DB_NAME'=>'db_database30',//数据库名称
'DB_PORT'=>'3306',//数据库端口
'DB_PREFIX'=>'tb_',//数据表前缀
);
?>
3、编写控制文件
<?php
header("content-type:text/html;charset=utf-8");//设置页面编码格式
class IndexAction extends Action{
public function index(){
$db=M('commo');//实例化模型类,参数为数据表名称,不包含前缀
if(isset($_GET['p'])){//判断地址栏是否有参数p
$page=$_GET['p'];
}else{
$page=1;
}
$result=$db->order('id')->page($page.',2')->select();//查询数据
$this->assign('result',$result);//模板变量赋值
//import("ORG.Util.Page");//导入分页类
import("@.ORG.Page");
$count=$db->count();//获取查询总记录数
$p=new Page($count,2);//实例化分页类,参数为总记录数和每页显示的记录数
$show=$p->show();//分页显示输出
$this->assign('show',$show);//模板变量赋值
$this->display();//输出模板
}
}
?>
4、编写模板文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>输出商品</title>
<link type="text/css" rel="stylesheet" href="__ROOT__/Public/Css/style.css">
</head>
<body>
<foreach name='result' item='commo'>
<table width="636" height="134" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#EEEEEE">
<tr>
<td width="145" rowspan="5" align="center" valign="middle"><img src="__ROOT__/Public/images/{$commo.pics}" width="90" height="100" alt="{$commo.name}" style="border: 1px solid #f0f0f0;" /></td>
<td height="26" align="left">商品名称:{$commo.name}</td>
<td width="156" height="26" align="left">商品类别:{$commo.class}</td>
<td width="157" height="26" align="left">商品型号:{$commo.model}</td>
</tr>
<tr>
<td height="26" align="left">商品品牌:{$commo.brand}</td>
<td height="26" colspan="2" align="left">商品产地:{$commo.area}</td>
</tr>
<tr>
<td width="178" height="26" align="left">剩余数量:{$commo.stocks}</td>
<td height="26" colspan="2" align="left">销售数量:{$commo.sell}</td>
</tr>
<tr>
<td height="26" align="left">市场价:<font color="red">{$commo.m_price} 元</font></td>
<td height="26" colspan="2" align="left">上市日期:{$commo.addtime}</td>
</tr>
<tr>
<td height="26" align="left">会员价格:<font color="#FF0000">{$commo.v_price} 元</font></td>
<td height="26" colspan="2" align="center" valign="middle"> </td>
</tr>
</table>
</foreach>
<table align="center">
<tr>
<td height="22" colspan="7" align="center" bgcolor="#DDDDDD"><span>{$show}</span></td>
</tr>
</table>
</body>
</html>
二 运行结果