把所有栏目从数据库取出,显示到页面
CatModel.class.php
<?php 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); } } |
商品分类:catelist.php
<?php define(‘ACC‘,true); require(‘../include/init.php‘); // 调用model $cat = new CatModel(); $catlist = $cat->select(); include(ROOT . ‘view/admin/templates/catelist.html‘); ?> |
前台页面view/admin/templates/catelist.html
<?php foreach($catlist as $v) { ?> <tr align="center" class="0" id="0_1" id = ‘tr_4‘> <td align="left" class="first-cell" style = ‘padding-left="1"‘> <img src="../view/admin/images/menu_minus.gif" id="icon_0_1" width="9" height="9" border="0" style="margin-left:<?php echo $v[‘lev‘]; ?>em" /> <span><a href="#" ><?php echo $v[‘cat_name‘]; ?></a></span> </td> <td width="10%">0</td> <td width="10%"><span> </span></td> <td width="10%"><img src="../view/admin/images/yes.gif" /></td> <td width="10%"><img src="../view/admin/images/yes.gif" /></td> <td><span>0</span></td> <td width="10%" align="right"><span>50</span></td> <td width="24%" align="center"> <a href="#">转移商品</a> | <a href="catedit.php?cat_id=<?php echo $v[‘cat_id‘];?>">编辑</a> | <a href="catedel.php?cat_id=<?php echo $v[‘cat_id‘];?>" onclick="if(!confirm(‘确实要删除吗?‘)){return false;};" title="移除">移除</a> </td> </tr> <?php } ?> |
cateadd.html
<td> <select name="parent_id"> <option value="0">顶级分类</option> <?php foreach($catlist as $v) { ?> <option value="<?php echo $v[‘cat_id‘]; ?>"> <?php echo str_repeat(‘ ‘,$v[‘lev‘]),$v[‘cat_name‘]; ?></option> <?php } ?> </select> </td> |
增加CatModel.class.php 方法: getCatTree获取子树
/* 指定栏目的id,返回$id栏目下的子孙树 需要无限极分类的知识 */ public function getCatTree($arr,$id = 0 ) { $tree = array(); foreach($arr as $v) { if($v[‘parent_id‘] == $id) { $tree[] = $v; $tree = array_merge($tree,$this->getCatTree($arr,$v[‘cat_id‘] )); } } return $tree; } |
算得出
<?php define(‘ACC‘,true); require(‘../include/init.php‘); // 调用model $cat = new CatModel(); $catlist = $cat->select(); $catlist = $cat->getCatTree($catlist,0); //print_r($catlist); include(ROOT . ‘view/admin/templates/catelist.html‘); ?> |
增加缩进功能CatModel.class.php
/* 指定栏目的id,返回$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; } |
修改缩进
catelist.html
<img src="../view/admin/images/menu_minus.gif" id="icon_0_1" width="9" height="9" border="0" style="margin-left:<?php echo $v[‘lev‘]; ?>em" /> |
cateadd.html
<?php echo str_repeat(‘ ‘,$v[‘lev‘]),$v[‘cat_name‘]; ?></option> |
本文出自 “杜国栋个人PHP学习博文” 博客,请务必保留此出处http://duguodong.blog.51cto.com/7667978/1387678
时间:2014年3月31日20:11:07栏目列表,布布扣,bubuko.com
原文:http://duguodong.blog.51cto.com/7667978/1387678