2016-07-07 22 views
0

私は2つのテーブルがある - このようになり、画像とカテゴリ: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; ?> 

私の目標は、DBから見るためにディナミカルフェッチを行うことです。

P.S:悪い日本

答えて

0

申し訳ありませんがあなたがwhere句を忘れてしまったようだすべての画像が返される理由thatsの。

はこのような機能の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

ガレリーモデルでこの機能を追加します。コード次

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

には、モデル、コントローラとビューを追加し、すべてのコードの上

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

ビューファイルmain.phpに追加します。完璧な出力を得ることができます。

+0

完璧に動作します。どうもありがとうございました。 –

+0

他の回答を受け入れる他の人がこの質問と回答に役立つことがあります。ありがとう。 –

関連する問題