【发布时间】:2016-08-21 14:25:15
【问题描述】:
我有三个类别,例如故事、电影故事、照片
我想从数据库中显示数据类别明智的数据......喜欢
1. 故事 *数据1 *数据2 *数据3
2.电影故事 *数据1 *数据2 *数据3
控制器:
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 (
id int(11) NOT NULL AUTO_INCREMENT,
p.id int(11) 默认为空,
c_id int(11) 默认为空,
category_title 文字,
category_url 文字,
category_image 文字,
主键 (id)
) ENGINE=InnoDB AUTO_INCREMENT=14 默认字符集=latin1
类别: id int(11) 非空 类别名称文本 NULL
我试过了,我想按类别显示数据:
【问题讨论】:
-
到目前为止你尝试过什么?
-
我给你发了图片
标签: php