2016-07-07 106 views
0

我有兩個表 - 圖像和類別誰是這樣的:如何使用Codeigniter過濾db數據?

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類,我需要過濾和separatly每個類別

我嘗試喜歡這一點,但它的顯示圖像顯示所有圖像

**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; ?> 

所以我的目標是從數據庫中進行dinamical fetch來查看。

P.S:對不起,壞的日本

回答

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; 
} 
+0

未定義的變量:categoryId –

2

你可以嘗試爲您解決這個代碼..

Category_model.php

在cagetory模型

添加此功能
//Model Category 
public function get_category(){ 
    $q = "select * from categories"; 
    $rs = $this->db->query($q); 
    return $rs->result(); 
} 

Gallery_model.php

在gallary模型中添加此功能。

//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; 
} 

Gallery.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); 
} 

main.php

添加以下代碼在視圖文件main.php

<h3 class="svgbg">ORGANIZERS</h3><!--name of the first category--> 
<?php foreach($category as $img) :?> 
<div> 
    <?php echo "Category Name: ".$img['title'];?> <br> 
    <?php foreach($img['r_images'] as $imgage) :?> 
     <div class="thumbnail"> 
      <?=img($imgage->file)?> <br> 
     </div> 
    <?php endforeach; ?> 
</div> 
<?php endforeach; ?> 

上面所有的代碼添加模型,控制器和視圖。你可以得到完美的輸出。

+0

完美的作品。非常感謝你。 –

+0

請接受其他答案可以幫助他們解答這個問題和答案。謝謝。 –