<?php
namespace Home\Model;
use Think\Model;
class ArticleCatModel extends Model {
/*
*分类树
*@param cat_id 分类的ID
*@param selected 当前选中选中的id或父id
*@param re_type 返回的类型: 值为真时返回下拉列表,否则返回数组
*@param is_show i如果为true显示所有分类,如果为false隐藏不可见分类
*/
public function article_cats($cat_id = 0, $selected = 0, $re_type=true, $is_show = true)
{
$cat_arr = array();
$cat_str = ""; //返回下拉列表
if($is_show)
{
$where['is_show'] = 1;
$where['parent_id'] = $cat_id;
}else
{
$where['parent_id'] = $cat_id;
}
//判断当前分类是否有子分类,如果有则循环取出子分类,如果没有,则取出当前分类
$parent_count = $this->where($where)->count();
if($parent_count>0 || $parent_id == 0)
{
$res = $this->where($where)->order('show_order, cat_id')->select();
foreach ($res as $key => $val)
{
if($val['cat_id'] == $selected)
{
$cat_str .= "<option selected='selected' value='".$val['cat_id']."'>".$val['cat_name']."</option>";
}else
{
$cat_str .= "<option value='".$val['cat_id']."'>".$val['cat_name']."</option>";
}
$cat_arr[$key]['cat_id'] = $val['cat_id'];
$cat_arr[$key]['cat_name'] = $val['cat_name'];
$cat_arr[$key]['level'] = 0;
if (isset($val['cat_id']) != NULL)
{
if($re_type)
{
$cat_str .= $this->article_cats_child($val['cat_id'], $selected, $re_type, $is_show);
}else
{
$cat_arr[$key]['cat_child'] = $this->article_cats_child($val['cat_id'], $selected, $re_type, $is_show);
}
}
}
}
if($re_type)
{
return $cat_str;
}else
{
return $cat_arr;
}
}
/*
*分类树
*@param cat_id 父分类的ID
*@param selected 当前选中选中的id或父id
*@param re_type 返回的类型: 值为真时返回下拉列表,否则返回数组
*@param is_show i如果为true显示所有分类,如果为false隐藏不可见分类
*@param level 级数
*/
public function article_cats_child($parent_id = 0, $selected = 0, $re_type=true, $is_show = true, $level = 0)
{
$level_str = ""; //├,└
$level++;
for($i=0; $i<$level; $i++)
{
$level_str .= "└";
}
if($is_show)
{
$where['is_show'] = 1;
$where['parent_id'] = $parent_id;
}else
{
$where['parent_id'] = $parent_id;
}
//判断当前分类是否有子分类,如果有则循环取出子分类
$parent_count = $this->where($where)->count();
if($parent_count>0 || $parent_id == 0)
{
$res = $this->where($where)->order('show_order, cat_id')->select();
foreach ($res as $key => $val)
{
if($val['cat_id'] == $selected)
{
$cat_str .= "<option selected='selected' value='".$val['cat_id']."'>".$level_str.$val['cat_name']."</option>";
}else
{
$cat_str .= "<option value='".$val['cat_id']."'>".$level_str.$val['cat_name']."</option>";
}
$cat_arr[$key]['cat_id'] = $val['cat_id'];
$cat_arr[$key]['cat_name'] = $val['cat_name'];
$cat_arr[$key]['level'] = $level;
if (isset($val['cat_id']) != NULL)
{
if($re_type)
{
$cat_str .= $this->article_cats_child($val['cat_id'], $selected, $re_type, $is_show, $level);
}else
{
$cat_arr[$key]['cat_child'] = $this->article_cats_child($val['cat_id'], $selected, $re_type, $is_show, $level);
}
}
}
}
if($re_type)
{
return $cat_str;
}else
{
return $cat_arr;
}
}
/*
*@param cat_id 分类的ID
*@param is_show i如果为true显示所有分类,如果为false隐藏不可见分类
*/
public function article_cat_list($cat_id = 0, $is_show = true)
{
$cat_arr = array(); //返回数组
//取出所有的分类
if($is_show)
{
$where = "";
}else
{
$where['is_show'] = 1;
}
$cats = $this->where($where)->select();
$cat_arr = $this->_turn_arr($cats);
return $cat_arr;
}
/*
*分类数组按照分类树重新排列
*@param arr
*@param parent_id 父类id
*@param level level等级
*/
//static变量。静态变量仅在局部函数域中存在且只被初始化一次,当程序执行离开此作用域时,其值不会消失,会使用上次执行的结果。
static public $treeList = array(); //分类重组后的数组
public function _turn_arr($arr, $parent_id = 0, $level = 0)
{
$level_str = "";
for($i=0; $i<$level; $i++)
{
$level_str .= "└";
}
foreach ($arr as $key => $val)
{
if($val['parent_id']==$parent_id)
{
$val['level_str'] = $level_str;
$val['level'] = $level;
self::$treeList[] = $val;
unset($arr[$key]);
$this->_turn_arr($arr, $val['cat_id'], $level+1);
}
}
return self::$treeList;
}
}
?>
namespace Home\Model;
use Think\Model;
class ArticleCatModel extends Model {
/*
*分类树
*@param cat_id 分类的ID
*@param selected 当前选中选中的id或父id
*@param re_type 返回的类型: 值为真时返回下拉列表,否则返回数组
*@param is_show i如果为true显示所有分类,如果为false隐藏不可见分类
*/
public function article_cats($cat_id = 0, $selected = 0, $re_type=true, $is_show = true)
{
$cat_arr = array();
$cat_str = ""; //返回下拉列表
if($is_show)
{
$where['is_show'] = 1;
$where['parent_id'] = $cat_id;
}else
{
$where['parent_id'] = $cat_id;
}
//判断当前分类是否有子分类,如果有则循环取出子分类,如果没有,则取出当前分类
$parent_count = $this->where($where)->count();
if($parent_count>0 || $parent_id == 0)
{
$res = $this->where($where)->order('show_order, cat_id')->select();
foreach ($res as $key => $val)
{
if($val['cat_id'] == $selected)
{
$cat_str .= "<option selected='selected' value='".$val['cat_id']."'>".$val['cat_name']."</option>";
}else
{
$cat_str .= "<option value='".$val['cat_id']."'>".$val['cat_name']."</option>";
}
$cat_arr[$key]['cat_id'] = $val['cat_id'];
$cat_arr[$key]['cat_name'] = $val['cat_name'];
$cat_arr[$key]['level'] = 0;
if (isset($val['cat_id']) != NULL)
{
if($re_type)
{
$cat_str .= $this->article_cats_child($val['cat_id'], $selected, $re_type, $is_show);
}else
{
$cat_arr[$key]['cat_child'] = $this->article_cats_child($val['cat_id'], $selected, $re_type, $is_show);
}
}
}
}
if($re_type)
{
return $cat_str;
}else
{
return $cat_arr;
}
}
/*
*分类树
*@param cat_id 父分类的ID
*@param selected 当前选中选中的id或父id
*@param re_type 返回的类型: 值为真时返回下拉列表,否则返回数组
*@param is_show i如果为true显示所有分类,如果为false隐藏不可见分类
*@param level 级数
*/
public function article_cats_child($parent_id = 0, $selected = 0, $re_type=true, $is_show = true, $level = 0)
{
$level_str = ""; //├,└
$level++;
for($i=0; $i<$level; $i++)
{
$level_str .= "└";
}
if($is_show)
{
$where['is_show'] = 1;
$where['parent_id'] = $parent_id;
}else
{
$where['parent_id'] = $parent_id;
}
//判断当前分类是否有子分类,如果有则循环取出子分类
$parent_count = $this->where($where)->count();
if($parent_count>0 || $parent_id == 0)
{
$res = $this->where($where)->order('show_order, cat_id')->select();
foreach ($res as $key => $val)
{
if($val['cat_id'] == $selected)
{
$cat_str .= "<option selected='selected' value='".$val['cat_id']."'>".$level_str.$val['cat_name']."</option>";
}else
{
$cat_str .= "<option value='".$val['cat_id']."'>".$level_str.$val['cat_name']."</option>";
}
$cat_arr[$key]['cat_id'] = $val['cat_id'];
$cat_arr[$key]['cat_name'] = $val['cat_name'];
$cat_arr[$key]['level'] = $level;
if (isset($val['cat_id']) != NULL)
{
if($re_type)
{
$cat_str .= $this->article_cats_child($val['cat_id'], $selected, $re_type, $is_show, $level);
}else
{
$cat_arr[$key]['cat_child'] = $this->article_cats_child($val['cat_id'], $selected, $re_type, $is_show, $level);
}
}
}
}
if($re_type)
{
return $cat_str;
}else
{
return $cat_arr;
}
}
/*
*@param cat_id 分类的ID
*@param is_show i如果为true显示所有分类,如果为false隐藏不可见分类
*/
public function article_cat_list($cat_id = 0, $is_show = true)
{
$cat_arr = array(); //返回数组
//取出所有的分类
if($is_show)
{
$where = "";
}else
{
$where['is_show'] = 1;
}
$cats = $this->where($where)->select();
$cat_arr = $this->_turn_arr($cats);
return $cat_arr;
}
/*
*分类数组按照分类树重新排列
*@param arr
*@param parent_id 父类id
*@param level level等级
*/
//static变量。静态变量仅在局部函数域中存在且只被初始化一次,当程序执行离开此作用域时,其值不会消失,会使用上次执行的结果。
static public $treeList = array(); //分类重组后的数组
public function _turn_arr($arr, $parent_id = 0, $level = 0)
{
$level_str = "";
for($i=0; $i<$level; $i++)
{
$level_str .= "└";
}
foreach ($arr as $key => $val)
{
if($val['parent_id']==$parent_id)
{
$val['level_str'] = $level_str;
$val['level'] = $level;
self::$treeList[] = $val;
unset($arr[$key]);
$this->_turn_arr($arr, $val['cat_id'], $level+1);
}
}
return self::$treeList;
}
}
?>