改进父类Model
<?php defined(‘ACC‘)||exit(‘ACC Denied‘); class Model { protected $table = NULL; // 是model所控制的表 protected $db = NULL; // 是引入的mysql对象 protected $pk; // 表的主键 public function __construct() { $this->db = mysql::getIns(); } public function table($table) { $this->table = $table; } /* 设计最基本的增删改操作 */ /* * 增加操作 * 传递一个数据数组,返回执行结果*/ public function add($data){ return $this->db->autoExecute($this->table,$data); } /* * 删除操作 *传递一个主键$id 返回受影响函数 */ public function delete($id){ $sql = ‘delete from ‘.$this->table.‘ where ‘.$this->pk.‘ = ‘.$id; /*删除是否成功,查看受影响的行数 存在两种失败的情况 1. 语句执行成功,但是没有影响行 2.语句没有执行成功 */ if($this->db->query($sql)){ return $this->db->affected_rows(); }else{ return false; } } /* * 修改操作: * param array $data 数组类型的 * param int $id * return int 影响函数 */ public function update($data,$id){ $rs= $this->autoExecute($this->table,$data,‘update‘,‘where ‘.$this->pk.‘ = ‘.$id); /*判断执行情况*/ if($this->db->query( $rs)){ return $this->db->affected_rows(); }else{ return false; } } /*查询数据,把所有数据查询出来 */ public function select(){ $sql = ‘select * from ‘ .$this->table; return $this->db->getAll($sql); } /*查询一行数据*/ public function find($id){ $sql = ‘select * from ‘.$this->table.‘ where ‘. $this->pk.‘ = ‘.$id; return $this->db->getRow($sql); } } |
GoodsModel.class.php
<?php defined(‘ACC‘)||exit(‘ACC Denied‘); class GoodsModel extends Model{ protected $table = ‘goods‘; protected $pk = ‘goods_id‘; } ?> |
goodslist.php
define(‘ACC‘,true); require(‘../include/init.php‘); $goods = new GoodsModel(); $goodslist = $goods->select(); include(ROOT . ‘view/admin/templates/goodslist.html‘); |
‘view/admin/templates/goodslist.html
<table cellpadding="3" cellspacing="1"> <tr> <th> <input onclick=‘listTable.selectAll(this, "checkboxes")‘ type="checkbox" /> <a href="#">编号</a><img src="../view/admin/images/sort_desc.gif"/> </th> <th><a href="#">商品名称</a></th> <th><a href="#">货号</a></th> <th><a href="#">价格</a></th> <th><a href="#">上架</a></th> <th><a href="#">精品</a></th> <th><a href="#">新品</a></th> <th><a href="#">热销</a></th> <th><a href="#">推荐排序</a></th> <th><a href="#">库存</a></th> <th>操作</th> <tr> <?php foreach($goodslist as $k){?> <tr> <td><input type="checkbox" name="checkboxes[]" value="32" /><?php echo $k[‘goods_id‘]?></td> <td class="first-cell" style=""><span ><?php echo $k[‘goods_name‘]?></span></td> <td><span ><?php echo $k[‘goods_sn‘]?></span></td> <td align="right"><span ><?php echo $k[‘shop_price‘]?> </span></td> <td align="center"><img src="../view/admin/images/yes.gif" /></td> <td align="center"><img src="../view/admin/images/yes.gif" /></td> <td align="center"><img src="../view/admin/images/yes.gif" /></td> <td align="center"><img src="../view/admin/images/yes.gif" /></td> <td align="center"><span >100</span></td> <td align="right"><span ><?php echo $k[‘goods_number‘]?></span></td> <td align="center"> <a href="goods.php?goods_id=<?php echo $k[‘goods_id‘]?>" target="_blank" title="查看"><img src="../view/admin/images/icon_view.gif" width="16" height="16" border="0" /></a> <a href="#" title="编辑"><img src="../view/admin/images/icon_edit.gif" width="16" height="16" border="0" /></a> <a href="#" title="复制"><img src="../view/admin/images/icon_copy.gif" width="16" height="16" border="0" /></a> <a href="#" title="回收站"><img src="../view/admin/images/icon_trash.gif" width="16" height="16" border="0" /></a> <a href="#" title="货品列表"><img src="../view/admin/images/icon_docs.gif" width="16" height="16" border="0" /></a> </td> </tr> <?php }?> </table> |
利用完善后的Model完成了商品添加和列表显示
本文出自 “杜国栋个人PHP学习博文” 博客,请务必保留此出处http://duguodong.blog.51cto.com/7667978/1388669
时间: 2014年4月1日20:54:11完善Model完成商品列表功能,布布扣,bubuko.com
时间: 2014年4月1日20:54:11完善Model完成商品列表功能
原文:http://duguodong.blog.51cto.com/7667978/1388669