商城实战之后台栏目管理(二)
<?php
/****
燕十八 公益PHP讲堂
论 坛: http://www.zixue.it
微 博: http://weibo.com/Yshiba
YY频道: 88354001
****/
class CatModel extends Model{
protected $table = 'category';
/*
你给我一个关键数组,键->表中的列,值-->表中的值,
add()函数自动插入该行数据
*/
public function add($data) {
return $this->db->autoExecute($this->table,$data);
}
// 获取本表下面所有的数据
public function select() {
$sql = 'select cat_id,cat_name,parent_id from ' . $this->table;
return $this->db->getAll($sql);
}
// 根据主键 取出一行数据
public function find($cat_id) {
$sql = 'select * from category where cat_id=' . $cat_id;
return $this->db->getRow($sql);
}
/*
getCatTree
pram: int $id
return $id栏目的子孙树
*/
public function getCatTree($arr,$id = 0,$lev=0) {
$tree = array();
foreach($arr as $v) {
if($v['parent_id'] == $id) {
$v['lev'] = $lev;
$tree[] = $v;
$tree = array_merge($tree,$this->getCatTree($arr,$v['cat_id'],$lev+1));
}
}
return $tree;
}
// 删除栏目
public function delete($cat_id=0) {
$sql = 'delete from ' . $this->table . ' where cat_id=' . $cat_id;
$this->db->query($sql);
return $this->db->affected_rows();
}
//
public function update($data,$cat_id=0) {
$this->db->autoExecute($this->table,$data,'update',' where cat_id=' . $cat_id);
return $this->db->affected_rows();
}
}
<?php
/****
燕十八 公益PHP讲堂
论 坛: http://www.zixue.it
微 博: http://weibo.com/Yshiba
YY频道: 88354001
****/
defined('ACC')||exit('ACC Denied');
class Model {
protected $table = NULL; // 是model所控制的表
protected $db = NULL; // 是引入的mysql对象
public function __construct() {
$this->db = mysql::getIns();
}
public function table($table) {
$this->table = $table;
}
}