我有三个类别,如故事,电影故事,照片
我想从数据库中显示数据类别明智的数据....比如
1.Stories *数据1 * DATA2 * DATA3
2.movie故事 *数据1 * DATA2 * DATA3
控制器:
function __construct(){
parent::__construct();
$this->load->model('home_model');
}
public function index()
{
$data['header_menu'] = $this->home_model->get_header_menu();
$data['category'] = $this->home_model->get_category();
$data['home_cat'] = $this->home_model->get_cat_data();
$this->load->view('home/header',$data);
$this->load->view('home/homepage');
$this->load->view('home/footer');
}
模型:
function __construct(){
parent::__construct();
}
function get_header_menu(){
$query = $this->db->get('header_menu');
return $query->result_array();
}
function get_category(){
$query = $this->db->get('category');
return $query->result_array();
}
function get_cat_data(){
$this->db->select('*');
$this->db->from('category');
$this->db->join('home_page','category.id = home_page.c_id','left');
$this->db->where('category.id = home_page.c_id');
$this->db->group_by('home_page.id');
$query = $this->db->get();
return $query->result_array();
}
查看:
<div class="container">
<div class="row">
<?php foreach($category as $cate) { ?>
<div class="col-md-4">
<div class="table-responsive">
<table class="table table-bordered table-bordered">
<tr>
<th class="success"><?php echo $cate['categories']?></th>
</tr>
<tr>
<td><?php foreach ($home_cat as $homecat){?>
<a href="#"><?php echo $homecat['category_title']?></a>
<hr>
<?php } ?></td>
</tr>
</table>
</div>
</div>
<?php } ?>
</div>
数据库:
创建表格 home_page:
CREATE TABLE home_page
(
id
int(11)NOT NULL AUTO_INCREMENT,
p.id
int(11)DEFAULT NULL,
c_id
int(11)DEFAULT NULL,
category_title
文字,
category_url
文字,
category_image
文字,
主要关键(id
)
)ENGINE = InnoDB AUTO_INCREMENT = 14 DEFAULT CHARSET = latin1
类别: id int(11)NOT NULL categories_name text NULL
我尝试了什么,我希望按类别显示数据:
答案 0 :(得分:0)
这不是你提高这个问题的确切答案
你必须像这样过滤视图页面中的值
在每个foreach循环中添加if条件以过滤类似
的类别<?php foreach($catagory as $cata) { ?>
<td><?php foreach ($home_cat as $homecat){
if($cata['id']==$homecat['c_id'])
{
//here we filter the value categorywise
?>
<a href="#"><?php echo $homecat['catagory_title']; ?></a>
<?php } ?></td>
<?php }
} ?>