当你在数据库中存储了三级以上的数据树时,你想把它显示在前端时,你会怎么做,下面是我在工作项目时遇到的一些思路(此项目是thinkphp5开发的)。
数据库:
demand_id是各分类的ID值,parent_id是用来做树的分类,第一级的parent_id的值是 0 ,第二级的parent_id值是其父级的demand_id值,第三级的parent_id值是其第二级父级的demand_id值,第四、第五同样往后推...
Model:
//获取三级分类
public function getTree() {
$fields = 'demand_id,demand_name,parent_id,sort,status';
$result = Db::name('demand_class')->field($fields)->where(array('parent_id' => 0, 'status' => 'Y'))->order('parent_id asc, sort asc')->select();
if (!$result)
return false;
$rs = array();
//顶级不显示图片 ,二级不显示
foreach ($result as $v) {
$cate_id = $v['demand_id'];
$rs[$cate_id]['cate_id'] = $v['demand_id'];
$rs[$cate_id]['cate_name'] = $v['demand_name'];
$data = $this->_son_cate($v);
if ($data) {
foreach ($data as $key => $val) {
// if (!isset($val['child']))
// continue; //是否有字分类
//去除二级中图片
//unset($val['url']);
$rs[$cate_id]['chilid'][] = $val;
}
}
}
foreach ($rs as $vv) {
$info[] = $vv;
}
return $info;
}
private function _son_cate($pcate_info) {
$pcate_id = $pcate_info['demand_id'];
$fields = 'demand_id,demand_name,parent_id,sort,status';
$result = Db::name('demand_class')->field($fields)->where(array('parent_id' => $pcate_id))->order('parent_id asc,sort asc')->select();
if (!$result)
return false;
foreach ($result as $v) {
$cate_id = $v['demand_id'];
$rs[$cate_id]['cate_id'] = $v['demand_id'];
$rs[$cate_id]['cate_name'] = $v['demand_name'];
$data = $this->_son_cate($v);
if ($data) {
foreach ($data as $value) {
if (!is_file(BASE_UPLOAD_PATH . '/' . ATTACH_COMMON . '/category-pic-' . $value['cate_id'] . '.jpg')) {
$value['image'] = PHONE_TEMPLATES . '/' . images . '/' . 'default_goods_image.gif';
} else {
$value['image'] = UPLOAD_SITE_URL . '/' . ATTACH_COMMON . '/category-pic-' . $value['cate_id'] . '.jpg';
}
// $value['url'] = htmlspecialchars_decode($value['gc_link']);
$rs[$cate_id]['child'][] = $value;
}
}
}
return $rs;
}
Contraller:
public function edit() {
//分类
$md_goods_class = Model('demand_class');
$denamd_class = $md_goods_class->getTree();
$this->assign('denamd_class', $denamd_class);
return $this->fetch('demand_from');
}
前端 demand_from.html
<select id='ac_id' name="ac_id">
<option value="">点击选择发布类别</option>
<?php $ac_id=$output['list']['ac_id']; if(!empty($output['denamd_class'])&&is_array($output['denamd_class'])){?>
<?php foreach($output['denamd_class'] as $val){?>
<option value="<?php echo $val['cate_id']?>"<?php if($val['cate_id']==$ac_id){echo 'selected="selected"';}?> ><?php echo $val['cate_name'] ?></option>
<?php if(!empty($val['chilid'])&&is_array($val['chilid'])){?>
<?php foreach($val['chilid'] as $v1){?>
<option value="<?php echo $v1['cate_id']?>" <?php if($v1['cate_id']==$ac_id){echo 'selected="selected"';}?>> <?php echo $v1['cate_name'] ?></option>
<?php if(!empty($v1['child'])&&is_array($v1['child'])){?>
<?php foreach($v1['child'] as $v2){?>
<option value="<?php echo $v2['cate_id']?>" <?php if($v2['cate_id']==$ac_id){echo 'selected="selected"';}?>> <?php echo $v2['cate_name'] ?></option>
<?php }?>
<?php }?>
<?php }?>
<?php }?>
<?php }?>
<?php }?>
</select>
我之前遇到的问题:
当我在前端看的时候发现cate_id 这个字段,我去它的控制器中查找,找到了它使用的demand_class数据表,然后去数据表里看发现表里并没有这个字段,通过我认真的查找我发现这个字段是从模型Model中定义来的。
注:文章来源雨中笑记录实习期遇到的问题与心得,转载请申明原文
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。