|
比如说:
正常的php文件里这么写可以调用出 分类列表里的每一个数据里都显示其父级分类的名字。
<?php
//数据库连接代码省略
//cid 是分类的id, parent_id是父级分类的id
$sql ="select * from category";
$query = mysql_query($sql);
$rs = mysql_fetch_array($query);
foreach($rs as $key):
$sql ="select * from category where cid =".$key['parent_id'].";
$query = mysql_query($sql);
$rs = mysql_fetch_array($query);
echo $rs['cname'];
endforeach;
?>
现在CI里是这么写的
model层:
//分类列表
public function getCategoryList(){
$data = $this->db->get('category')->result_array();
return $data;
}
//查看指定分类
public function getCategoryById($cat_id = FALSE){
$data = $this->db->where(array('cat_id'=>$cat_id))->get('category')->result_array();
return $data;
}
control层:
public function __construct(){
parent::__construct();
$this->load->model('Category_model', 'cat');
}
function index() {
//列表循环
$data['category'] = $this->cat->getCategoryList();
$data['links'] = $this->pagination->create_links();
$data['parents'] = $this->cat->getCategoryById();
$this->load->view('category', $data);
}
view层:
<table class="table table-bordered table-striped with-check">
<thead>
<tr>
<th>ID</i></th>
<th>分类名称</th>
<th>父级分类</th>
<th>导航</th>
<th>操作</th>
</tr>
</thead>
<?php foreach($category as $v): ?>
<tr>
<td><?php echo $v['cat_id']; ?></td>
<td><?php echo $v['cat_name']; ?></td>
<td>
<!-- 想在这里写个循环 调用控制器的$date['parent'], 但如何把where条件cat_id = parent_id 传进去呢? -->
<?php echo $v['parent_id']; ?>
</td>
<td><?php echo $v['cat_nav']==0?"普通分类":"导航分类"; ?></td>
<td class="center">
<a href="<?php echo site_url('category/edit/'.$v['cat_id']); ?>" class="btn btn-success btn-mini">编辑分类</a>
<a href="<?php echo site_url('category/remove/'.$v['cat_id']); ?>" class="btn btn-danger btn-mini">删除分类</a>
</td>
</tr>
<?php endforeach; ?>
新手求解答。谢谢了! |
|