军哥谈CI框架之无限分类类库的实现和应用

大家好,,我是军哥,英文名:JayJun,一直想跟大伙交流一下学习和使用CI的心得和经验,最近也在用CI写一个在线书城项目,已经完成80%,其中有用到无限分类,关于无限分类,有许多的实现方式,今个呢,军哥,跟大家先分享自己写的无限分类类库,只适合CI框架哟,当然你也可以修改后使用到其它地方,接着我们会在CI框架中应用一下(详见附件中代码示例)。这里要求你有一定的面向对象基础,当然了解和熟悉CI(或其它PHP框架)就更好啦。


另外,军哥在代码中应用了市面上热门的一个前端UI框架——bootstarp,这个框架在为我们实现页面样式方面是表现的相当给力!详见:bootstrap前端UI框架中文版官网



好了,不废话啦,军哥语文学滴不好(小学没少挨老师打手掌心呀),就直接上代码啦!


有学习和研究CI的,欢迎拍砖!!! 代码示例 CI_cate.rar 1、先看效果,有图有真相;
分类显示页:


添加分类页:


编辑分类页:


2、控制器( 源码在 application/controllers 文件夹)
  1. //无限分类控制器功能
  2. class cate extends CI_Controller
  3. {
  4. private $_cate_url = 'cate/'; //无限分类视图路径
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->base_url = $this->config->item("base_url");
  9. $this->load->library('category');
  10. }
  11. //显示分类
  12. public function index()
  13. {
  14. $data['base_url'] = $this->base_url;
  15. $data['tree_str'] = $this->category->getListStr();
  16. $this->load->view($this->_cate_url.'cate_index',$data);
  17. }
  18. //编辑分类
  19. public function edit($cid = '')
  20. {
  21. $data['base_url'] = $this->base_url;
  22. if ($cid != '')
  23. {
  24. $query = $data['post'] = $this->category->fetchOne($cid);
  25. $pid = $query['fatherId'];
  26. $data['option_str'] = $this->category->getOptionStr(0,true,0,false,$pid);
  27. }
  28. else
  29. {
  30. $data['option_str'] = $this->category->getOptionStr(0,true,0,false,0);
  31. }
  32. $this->load->view($this->_cate_url.'cate_edit',$data);
  33. }
  34. //执行插入分类操作
  35. public function insert()
  36. {
  37. //获取表单提交的信息
  38. $fatherId = $this->input->post('fatherId');
  39. $cateName = $this->input->post('cateName');
  40. $content = $this->input->post('content');
  41. $sort = $this->input->post('sort');
  42. $display = $this->input->post('display');
  43. if ($this->category->addCategory($fatherId,$cateName,$content,$sort,$display) > 0)
  44. {
  45. echo "<script language=\"javascript\">alert('添加成功!')</script>";
  46. }
  47. else
  48. {
  49. echo "<script language=\"javascript\">alert('添加失败!')</script>";
  50. }
  51. echo "<script language=\"javascript\">history.go(-1);</script>";
  52. }
  53. //执行编辑操作
  54. function update($cid = '')
  55. {
  56. $cid = ($cid === '') ? $this->input->post('cid') : $cid;
  57. if ($cid !== '')
  58. {
  59. //获取表单提交的信息
  60. $fatherId = $this->input->post('fatherId');
  61. $cateName = $this->input->post('cateName');
  62. $content = $this->input->post('content');
  63. $sort = $this->input->post('sort');
  64. $display = $this->input->post('display');
  65. if ($this->category->editCategory($cid,$fatherId,$cateName,$content,$sort,$display) > 0)
  66. {
  67. echo "<script language=\"javascript\">alert('更新成功!')</script>";
  68. }
  69. else
  70. {
  71. echo "<script language=\"javascript\">alert('更新失败!')</script>";
  72. }
  73. }
  74. echo "<script language=\"javascript\">history.go(-1);</script>";
  75. }
  76. //执行删除操作
  77. function delete($cid = '')
  78. {
  79. if ($cid !== '')
  80. {
  81. if ($this->category->delCategory($cid) > 0)
  82. {
  83. echo "<script language=\"javascript\">alert('删除成功!')</script>";
  84. }
  85. else
  86. {
  87. echo "<script language=\"javascript\">alert('删除失败!')</script>";
  88. }
  89. }
  90. echo "<script language=\"javascript\">history.go(-1);</script>";
  91. }
  92. }


3、无限分类类库(源码在 application/libraries 文件夹);
/*==================================================================*/
  1. /* 文件名:Category.php */
  2. /* 功能:实现无限分类的增删改查,用于codeigniter框架,
  3. 也可以修改后用于其它用途。 */
  4. /* 作者:张礼军
  5. /* 英文名:JayJun QQ:413920268 */
  6. /* 创建时间:2012-08-29 */
  7. /* 最后修改时间:2012-08-31 */
  8. /* copyright (c)2012 jayjun0805@sina.com */
  9. /*==================================================================*/
  10. if (!defined('BASEPATH')) exit('No direct script access allowed');
  11. class Category {
  12. private $CI; //CI对象
  13. private $tableName; //要操作的表名
  14. //表的七个字段
  15. private $cid; //分类ID
  16. private $fatherId; //父分类ID
  17. private $cateName; //分类名称
  18. private $sort; //分类排序,在同一父级下有多级时,用于排序
  19. private $content; //分类介绍
  20. private $level; //分类等级,即当前目录的级别
  21. private $display; //分类显示状态
  22. //所取分类的深度
  23. private $depth = 0;
  24. private $startLevel = 0;
  25. /**
  26. * 构造函数
  27. * @param $arr 参数包括表名,及分类表的七个字段名,如果没有定义,则采用默认,
  28. * 默认值
  29. * 表名:category
  30. * 分类ID:cid
  31. * 父ID:fatherId
  32. * 分类名称:cateName
  33. * 分类排序:sort
  34. * 分类介绍:content
  35. * 分类等级:level
  36. * 分类显示状态:display
  37. */
  38. public function __construct($arr = array())
  39. {
  40. //通过引用的方式赋给变量来初始化原始的CodeIgniter对象
  41. $this->CI = &get_instance();
  42. //初始化表参数
  43. $this->tableName = (isset($arr['tableName'])) ? $arr['tableName'] : 'category';
  44. $this->cid = (isset($arr['cid'])) ? $arr['cid'] : 'cid';
  45. $this->fatherId = (isset($arr['fatherId'])) ? $arr['fatherId'] : 'fatherId';
  46. $this->cateName = (isset($arr['cateName'])) ? $arr['cateName'] : 'cateName';
  47. $this->sort = (isset($arr['sort'])) ? $arr['sort'] : 'sort';
  48. $this->content = (isset($arr['content'])) ? $arr['content'] : 'content';
  49. $this->level = (isset($arr['level'])) ? $arr['level'] : 'level';
  50. $this->display = (isset($arr['display'])) ? $arr['display'] : 'display';
  51. }
  52. /**
  53. * 从数据库取所有分类数据,返回数组
  54. */
  55. public function fetchData($display)
  56. {
  57. if ($display)
  58. {
  59. $query = $this->CI->db->get_where($this->tableName,array($this->display => 0));
  60. }
  61. else
  62. {
  63. $query = $this->CI->db->get($this->tableName);
  64. }
  65. return $query->result_array();
  66. }
  67. /**
  68. *取某一条分类数据
  69. *@param $cid 分类ID
  70. */
  71. public function fetchOne($cid)
  72. {
  73. $this->CI->db->where($this->cid,$cid);
  74. $query = $this->CI->db->get($this->tableName);
  75. return $query->row_array(1);
  76. }
  77. /**
  78. *取出所有分类信息,返回数组,包括分类名称,一般用在select标签中显示
  79. * @param $fatherId 父类ID
  80. * @param $withself 查下级分类的时候,是否包含自己,默认false不包含。
  81. * @param $depth 所取分类的深度,值为0表示不限深度,会取所有的子分类。
  82. * @param $display 分类显示状态,
  83. */
  84. public function getAllCategory($fatherId = 0,$withself = false,$depth = 0,$display = false)
  85. {
  86. $result = array();
  87. $resArr = $this->fetchData($display); //获取所有分类信息
  88. // p($resArr);
  89. if($fatherId == 0 && $withself)
  90. {
  91. $root = array(
  92. $this->cid => 0,
  93. $this->fatherId => -1,
  94. $this->cateName => '根目录',
  95. $this->level => 0,
  96. $this->sort => 0
  97. );
  98. array_unshift($resArr, $root);
  99. }
  100. //p($resArr);
  101. if (empty($resArr))
  102. {
  103. return array();
  104. }
  105. //取得根目录
  106. foreach($resArr as $item)
  107. {
  108. if ($item[$this->fatherId] == $fatherId)
  109. {
  110. $level = $item[$this->level];
  111. }
  112. if ($withself)
  113. {
  114. if ($item[$this->cid] == $fatherId)
  115. {
  116. $result[] = $item;
  117. $level = $item[$this->level];
  118. break;
  119. }
  120. }
  121. }
  122. if (!isset($level))
  123. {
  124. return array();
  125. }
  126. $this->depth = $depth;
  127. $this->startLevel = $level;
  128. $nextLevel = $withself ? ($level + 1) : $level;
  129. return array_merge($result,$this->getChildren($resArr,$fatherId,$nextLevel));
  130. }
  131. /**
  132. * 取出某一分类下的所有ID,返回数组,fatherId = 0为根目录
  133. * @param $fatherId 父类ID
  134. * @param $widthself 取子分类时,是否包含自己,默认不包含
  135. * @param $depth 要读取的层级深度,默认查出所有子分类
  136. */
  137. public function getAllCategoryId($fatherId = 0,$widthself = false,$depth = 0,$display = false)
  138. {
  139. $idArr = array();
  140. if ($widthself)
  141. {
  142. array_push($idArr,$fatherId);
  143. }
  144. $cate = $this->getAllCategory($fatherId,$widthself,$depth,$display);
  145. foreach($cate as $item)
  146. {
  147. $idArr[] = $item[$this->cid];
  148. }
  149. return $idArr;
  150. }
  151. /**
  152. * 用于在下拉列表框中使用
  153. * @param $fatheriId 父类ID
  154. * @param $widthself 若取子分类的时候是否获取本身
  155. * @param $depth 分类深度
  156. * @param $display 分类显示状态
  157. * @param $selectId 用于编辑分类时自动设置默认状态为selected
  158. */
  159. public function getOptionStr($fatherId = 0,$withself = false,$depth = 0,$display = false,$selectId = 0)
  160. {
  161. $str = '';
  162. $cate = $this->getAllcategory($fatherId,$withself,$depth,$display);
  163. if (!empty($cate))
  164. {
  165. $line = '┣';
  166. foreach($cate as $item)
  167. {
  168. $selected = '';
  169. if ($selectId != 0 && $item[$this->cid] == $selectId)
  170. {
  171. $selected = 'selected';
  172. }
  173. $str .= '<option '.$selected.' value="'.$item[$this->cid].'">'.$line.str_repeat('━',($item[$this->level] - $this->startLevel)*2).$item[$this->cateName].'</option>';
  174. }
  175. }
  176. return $str;
  177. }
  178. /**
  179. * 用于列表显示,按ul li标签组织
  180. * @param $fatherId 父分类ID
  181. * @param $widthself 若取子分类的时候是否获取本身
  182. * @param $widthHref 是否提供超链接,即编辑和删除链接
  183. * @param $depth 分类深度
  184. */
  185. public function getListStr($fatherId = 0,$widthself = false,$withHref = true,$depth = 0,$display = false)
  186. {
  187. //开头
  188. $str = '';
  189. $startLevel = -1;
  190. $preLevel = 0;
  191. $cate = $this->getAllCategory($fatherId,$widthself,$depth,$display);
  192. if (!empty($cate))
  193. {
  194. foreach($cate as $item)
  195. {
  196. if ($startLevel < 0)
  197. {
  198. $startLevel = $item[$this->level];
  199. }
  200. if ($item[$this->level] < $preLevel) {
  201. $str .='
  202. '.str_repeat('</ul>',$preLevel - $item[$this->level]);
  203. }
  204. elseif ($item[$this->level] > $preLevel) {
  205. $str .='<ul>';
  206. }
  207. else
  208. {
  209. $str .='
  210. ';
  211. }
  212. if ($withHref && $item[$this->cid]!= 0)
  213. {
  214. $str .= '
  215. <span style="float:right;">
  216. '.($this->isDisplay($item[$this->cid]) ? "正常" : "待审").'
  217. <a href="'.site_url('cate/edit/'.$item[$this->cid]).'" class="mr50 ml200">edit
  218. <a οnclick=\'return confirm("Are your sure to delete?");\' href="'.site_url('cate/delete/'.$item[$this->cid]).'">del
  219. '.str_repeat(' ',($item[$this->level]-$this->startLevel)*4).'
  220. <span class="fb f16">'.($this->isChildren($item[$this->cid]) ? "+" : "-").'
  221. <input type="text" name="cname" class="span2" value="'.$item[$this->cateName].'" style="border:0px;" />';
  222. }
  223. else
  224. {
  225. $str .= '
  226. '.$item[$this->cateName];
  227. }
  228. $preLevel = $item[$this->level];
  229. }
  230. }
  231. //收尾
  232. $str .=str_repeat('
  233. </ul>',$preLevel - $startLevel + 1);
  234. return $str;
  235. }
  236. /**
  237. * 增加分类
  238. * @param $fatherId 父类ID
  239. * @param $cateName 分类名称
  240. * @param $content 分类介绍
  241. * @param $sort 分类排序, 只对同一级下的分类有用
  242. * @param $display 分类显示状态
  243. */
  244. public function addCategory($fatherId,$cateName,$content,$sort,$display)
  245. {
  246. //先获取父类的类别信息
  247. $parentInfo = $this->fetchOne($fatherId);
  248. //p($parentInfo);
  249. //获取分类的分类级别
  250. if (isset($parentInfo[$this->level]))
  251. {
  252. $level = $parentInfo[$this->level];
  253. }
  254. else
  255. {
  256. $level = 0;
  257. }
  258. $data = array(
  259. $this->fatherId => $fatherId,
  260. $this->cateName => $cateName,
  261. $this->content => $content,
  262. $this->sort => $sort,
  263. $this->level => $level + 1,
  264. $this->display => $display
  265. );
  266. $this->CI->db->insert($this->tableName,$data);
  267. return $this->CI->db->affected_rows();
  268. }
  269. /**
  270. * 删除分类
  271. * @param $cid 要删除的分类ID
  272. * @param $widthChild 是否删除下面的子分类,默认会删除
  273. */
  274. public function delCategory($cid,$widthChild = true)
  275. {
  276. if ($widthChild)
  277. {
  278. $idArr = $this->getAllCategoryId($cid,true);
  279. $this->CI->db->where_in($this->cid,$idArr);
  280. }
  281. else
  282. {
  283. $this->CI->db->where($this->cid,$cid);
  284. }
  285. $this->CI->db->delete($this->tableName);
  286. return $this->CI->db->affected_rows();
  287. }
  288. /**
  289. * 更新分类
  290. * @param $cid 要编辑的分类ID
  291. * @param $fatherId 父类ID
  292. * @param $cateName 分类的名称
  293. * @param $sort 分类排序
  294. * @param $display 分类显示状态
  295. */
  296. function editCategory($cid,$fatherId,$cateName,$content,$sort,$display)
  297. {
  298. //先获取父分类的信息
  299. $parentInfo = $this->fetchOne($fatherId);
  300. //获取当前等级
  301. if(isset($parentInfo[$this->level]))
  302. {
  303. $level = $parentInfo[$this->level];
  304. }
  305. else
  306. {
  307. $level = 0;
  308. }
  309. $currentInfo = $this->fetchOne($cid);
  310. //p($currentInfo);
  311. $newLevel = $level + 1;
  312. $levelDiff = $newLevel - $currentInfo[$this->level];
  313. //修改子分类的level
  314. if(0 != $levelDiff)
  315. {
  316. $childIdArr = $this->getAllCategoryId($cid);
  317. foreach($childIdArr as $item)
  318. {
  319. $this->CI->db->set($this->level, $this->level.'+'.$levelDiff, FALSE);
  320. $this->CI->db->where($this->cid, $item);
  321. $this->CI->db->update($this->tableName);
  322. }
  323. }
  324. //修改自己的信息
  325. $data = array(
  326. $this->fatherId => $fatherId,
  327. $this->cateName => $cateName,
  328. $this->level => $newLevel,
  329. $this->sort => $sort,
  330. $this->display => $display,
  331. );
  332. $this->CI->db->where($this->cid, $cid);
  333. $this->CI->db->update($this->tableName, $data);
  334. return $this->CI->db->affected_rows();
  335. }
  336. /**
  337. * 按顺序返回分类数组,用递归实现
  338. * @param unknown_type $cateArr
  339. * @param unknown_type $fatherId
  340. * @param unknown_type $level
  341. */
  342. private function getChildren($cateArr,$fatherId=0,$level = 1)
  343. {
  344. if($this->depth != 0 && ($level >=($this->depth + $this->startLevel)))
  345. {
  346. return array();
  347. }
  348. $resultArr = array();
  349. $childArr = array();
  350. //遍历当前父ID下的所有子分类
  351. foreach($cateArr as $item)
  352. {
  353. if($item[$this->fatherId] == $fatherId && ($item[$this->level] == $level))
  354. {
  355. //将子分类加入数组
  356. $childArr[] = $item;
  357. }
  358. }
  359. if(count($childArr) == 0)
  360. {
  361. //不存在下一级,无需继续
  362. return array();
  363. }
  364. //存在下一级,按sort排序先
  365. usort($childArr,array('Category','compareBysort'));
  366. foreach($childArr as $item)
  367. {
  368. $resultArr[] = $item;
  369. $temp = $this->getChildren($cateArr,$item[$this->cid],($item[$this->level] + 1));
  370. if(!empty($temp))
  371. {
  372. $resultArr = array_merge($resultArr, $temp);
  373. }
  374. }
  375. return $resultArr;
  376. }
  377. //比较函数,提供usort函数用
  378. private function compareBysort($a, $b)
  379. {
  380. if ($a == $b)
  381. {
  382. return 0;
  383. }
  384. return ($a[$this->sort] > $b[$this->sort]) ? +1 : -1;
  385. }
  386. //判断是否有子类别
  387. function isChildren($id)
  388. {
  389. //从数据库中取出只有fatherId字段的数据,返回数组
  390. $this->CI->db->select($this->fatherId);
  391. $query = $this->CI->db->get($this->tableName);
  392. $resArr = $query->result_array();
  393. foreach ($resArr as $v)
  394. {
  395. $arr[] = $v[$this->fatherId];
  396. }
  397. return (in_array($id,array_unique($arr))) ? true : false;
  398. }
  399. //判断状态是否启用
  400. function isDisplay($id)
  401. {
  402. $query = $this->fetchOne($id);
  403. return ($query[$this->display] == 1) ? true : false;
  404. }
  405. }

4、视图 源码在 application/views 文件夹

分类显示视图:
<html>
  1. <head>
  2. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  3. <title>Cate_index</title>
  4. <link rel="stylesheet" href="<?php echo $base_url; ?>/bootstrap/css/bootstrap.min.css" type="text/css" media="screen" charset="utf-8">
  5. <link rel="stylesheet" href="<?php echo $base_url; ?>/public/css/base.css" type="text/css" media="screen" charset="utf-8">
  6. </head>
  7. <body>
  8. <div class="w700 bc mt50">
  9. <h1 class="fb f20 mb20">分类列表显示</h1>
  10. <?php echo $tree_str ?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值