<?php
class category_model extends CI_Model{
public $cishop;
public function __construct(){
parent::__construct();
$this->cishop = $this->load->database('cishop',true);
}
/*
*$pid
*retrun array 节点所有后代节点
*/
public function get_data($table='ci_category',$where=array()){
if(!empty($where)){
$res = $this->cishop->get_where($table,$where)->result_array();
}else{
$res = $this->cishop->get_where($table,$where)->result_array();
}
return $res;
}
public function update_data( $where=array(), $data, $table='ci_category'){
$bool = $this->cishop->where($where)->update('ci_category',$data);
echo $this->cishop->last_query();
if($bool){
return true;
}else{
return false;
}
}
/**
*@param $arr 分类信息
*@param $pid 父id
*@return $arr 三维数组,前台分类数据
*
*/
public function front_cate(){
$data = $this->query_data('ci_category');
//var_dump($data);
return $this->cate_front_list($data);
}
public function cate_front_list($arr , $pid=0){
$child =$this->child($arr,$pid);
//var_dump($child);
if(empty($child)){
return null;
}else{
foreach($child as $k=>$v){
$current_child = $this->cate_front_list($arr, $v['cat_id']);
if(empty($current_child)){
}else{
//该分类节点下面还有子分类节点,则将子节点作为该节点元素保存
$child[$k]['child'] = $current_child;
//var_dump($child);
}
}
}
return $child;
}
public function child($arr, $pid=0){
$child= array();
foreach($arr as $v){
if($v['parent_id'] == $pid){
//$v['child'] = $this->cate_front_list($arr,$v['cat_id']);
$child[]=$v;
}
}
return $child;
}
public function query_data($table=''){
if(!empty($where)){
$res = $this->cishop->get($table)->result_array();
}else{
$res = $this->cishop->get($table)->result_array();
}
return $res;
}
public function list_cat($pid = 0){
$arr = $this->query_data('category');
//$pid=$arr[0]['parent_id'];
$cat_arr = $this-> _tree($arr,$pid);
return $cat_arr;
}
/*@access public
*@param $arr 要遍历的数组
*@param $id
*@param $level 层级
*
*/
public function _tree($arr, $id=0, $level=0){
//保存重组结果
static $tree = array();
foreach($arr as $v){
if($v['parent_id'] == $id){
$v['level'] = $level;
$tree[] = $v;
$cid = $v['cat_id'];
$this->_tree($arr,$cid,$level+1);
}
}
return $tree;
}
public function insert_cat(){
$parent_id = $this->input->post('parent_id');
$measure_unit = $this->input->post('measure_unit');
$sort_order = $this->input->post('sort_order');
$is_show = $this->input->post('is_show');
$show_in_nav = $this->input->post('show_in_nav');
$cat_recommend = $this->input->post('cat_recommend');
$cat_desc = $this->input->post('cat_desc');
$cat_name = $this->input->post('cat_name');
$data = array(
"cat_name" => $cat_name,
"parent_id" => $parent_id,
"cat_desc" => $cat_desc,
"sort_order" => $sort_order,
"unit" => $measure_unit,
"is_show" => $is_show
);
var_dump($data);
$bool = $this->cishop->insert("ci_category", $data);
return $bool;
}
}
?>
class category_model extends CI_Model{
public $cishop;
public function __construct(){
parent::__construct();
$this->cishop = $this->load->database('cishop',true);
}
/*
*$pid
*retrun array 节点所有后代节点
*/
public function get_data($table='ci_category',$where=array()){
if(!empty($where)){
$res = $this->cishop->get_where($table,$where)->result_array();
}else{
$res = $this->cishop->get_where($table,$where)->result_array();
}
return $res;
}
public function update_data( $where=array(), $data, $table='ci_category'){
$bool = $this->cishop->where($where)->update('ci_category',$data);
echo $this->cishop->last_query();
if($bool){
return true;
}else{
return false;
}
}
/**
*@param $arr 分类信息
*@param $pid 父id
*@return $arr 三维数组,前台分类数据
*
*/
public function front_cate(){
$data = $this->query_data('ci_category');
//var_dump($data);
return $this->cate_front_list($data);
}
public function cate_front_list($arr , $pid=0){
$child =$this->child($arr,$pid);
//var_dump($child);
if(empty($child)){
return null;
}else{
foreach($child as $k=>$v){
$current_child = $this->cate_front_list($arr, $v['cat_id']);
if(empty($current_child)){
}else{
//该分类节点下面还有子分类节点,则将子节点作为该节点元素保存
$child[$k]['child'] = $current_child;
//var_dump($child);
}
}
}
return $child;
}
public function child($arr, $pid=0){
$child= array();
foreach($arr as $v){
if($v['parent_id'] == $pid){
//$v['child'] = $this->cate_front_list($arr,$v['cat_id']);
$child[]=$v;
}
}
return $child;
}
public function query_data($table=''){
if(!empty($where)){
$res = $this->cishop->get($table)->result_array();
}else{
$res = $this->cishop->get($table)->result_array();
}
return $res;
}
public function list_cat($pid = 0){
$arr = $this->query_data('category');
//$pid=$arr[0]['parent_id'];
$cat_arr = $this-> _tree($arr,$pid);
return $cat_arr;
}
/*@access public
*@param $arr 要遍历的数组
*@param $id
*@param $level 层级
*
*/
public function _tree($arr, $id=0, $level=0){
//保存重组结果
static $tree = array();
foreach($arr as $v){
if($v['parent_id'] == $id){
$v['level'] = $level;
$tree[] = $v;
$cid = $v['cat_id'];
$this->_tree($arr,$cid,$level+1);
}
}
return $tree;
}
public function insert_cat(){
$parent_id = $this->input->post('parent_id');
$measure_unit = $this->input->post('measure_unit');
$sort_order = $this->input->post('sort_order');
$is_show = $this->input->post('is_show');
$show_in_nav = $this->input->post('show_in_nav');
$cat_recommend = $this->input->post('cat_recommend');
$cat_desc = $this->input->post('cat_desc');
$cat_name = $this->input->post('cat_name');
$data = array(
"cat_name" => $cat_name,
"parent_id" => $parent_id,
"cat_desc" => $cat_desc,
"sort_order" => $sort_order,
"unit" => $measure_unit,
"is_show" => $is_show
);
var_dump($data);
$bool = $this->cishop->insert("ci_category", $data);
return $bool;
}
}
?>