我有两张桌子 - 图片和类别看起来像这样:
CREATE TABLE `categories` (
`id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`user_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `images` (
`id` int(11) NOT NULL,
`categories_id` int(11) NOT NULL,
`file` text NOT NULL,
`caption` text NOT NULL,
`description` text NOT NULL,
`user_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我有4个类别,我需要过滤并分别显示每个类别的图像
我尝试这样但显示所有图像
**Model:**
public function org_filter()//filter categories
{
$this->db->select('i.file,i.categories_id,c.id,c.title',false);
$this->db->from('images as i');
$this->db->join('categories as c','i.categories_id = c.id','inner');//join tables based on the foreign key
$this->db->where('c.title','organizers');//set filter
$query = $this->db->get();//get data*/
return $query;
}
**Controller**
$data=array(
'r_images' => $this->Gallery_model->org_filter(),
);
$this->load->view('layouts/main', $data);
**View**
<h3 class="svgbg">ORGANIZERS</h3><!--name of the first category-->
<?php foreach($r_images->result() as $img) : ?>
<div>
<div class="thumbnail">
<?=img($img->file)?>
</div>
</div>
<?php endforeach; ?>
所以我的目标是从db中进行dinamical fetch来查看。
P.S:抱歉日本人不好
答案 0 :(得分:2)
您可以尝试使用此代码来解决问题..
<强> Category_model.php 强>
在笼子模型中添加此功能
var bl = require('bl');
var http = require('http')
var dataOne = http.get(process.argv[2], function(response){
response.pipe(bl(function(err, data) {
if (err)
return console.error(err)
data = data.toString()
return data
}))
})
console.log(dataOne)
<强> Gallery_model.php 强>
在gallary模型中添加此函数。
//Model Category
public function get_category(){
$q = "select * from categories";
$rs = $this->db->query($q);
return $rs->result();
}
<强> Gallery.php 强>
以下代码添加到图库控制器中。
//Gallery_model
public function org_filter($categoryId)//filter categories
{
$q = "SELECT * FROM images INNER JOIN categories on images.categories_id = categories.id WHERE categories.id =".$categoryId;
$q = $this->db->query($q)->result();
return $q;
}
<强> main.php 强>
以下代码添加到视图文件main.php
中function index()
{
$category = $this->dashboard_model->get_category();
$data = array();
//Controller
//$category = $this->Category_model->get_category();
foreach ($category as $key => $value) {
$data['category'][$key]['title'] = $value->title;
$data['category'][$key]['r_images'] = $this->dashboard_model->org_filter($value->id);
}
$this->load->view('layouts/main', $data);
}
首先代码添加你的模型,控制器和视图。你可以获得完美的输出。
答案 1 :(得分:0)
您似乎忘记了where子句,这就是为什么返回所有图像。
尝试更改函数org_filter,如下所示:
public function org_filter($categoryId)//filter categories
{
$this->db->select('*');
$this->db->from('images');
$this->db->join('categories','images.categories_id = categories.id','inner');//join tables based on the foreign key
$this->db->where('categories.id',$categoryId);
$this->db->on('images.id',10);//set filter
$query = $this->db->get();//get data
return $query;
}