2016-10-01 120 views
0

我有一個數據庫在mysql中有兩個表一個命名的類別(cat_id主鍵)和一個命名的書(b_id作爲主鍵)其中cat_id是書的外鍵。 我在CI工作,我在這裏輸出的類別視圖:如何將參數傳遞給控制器​​在codeIgniter

 foreach($categories->result()as $row){ 
 
      foreach($categories->result()as $row){ 
 
     echo '<a href="category_details?cat_id=$cat_id">'.$row->category.'</a><br>'; 
 

 
    } 
 

 
     }

我想要點擊的鏈接category_details時,只輸出該類別的書籍。 在這裏,我有我的控制器,這個方法:

public function category_details($data) 
 
    { 
 
     $data['cat_id'] = $this->home_model->output_cat_detail(); 
 
     $data['category_detail'] = $this->home_model->output_cat_detail();//printon librat 
 
     $data['categories'] = $this->home_model->output_categories(); 
 
     $this->load->view('header', $data); 
 
     $this->load->view('category_details', $data); 
 

 

 
    }

所以模型,它確實是選擇的方法是這樣的:

public function output_cat_detail(){ 
 
    $condition = "cat_id =" . "'" . $data['cat_id'] . "'"; 
 
    $this->db->select('*'); 
 
    $this->db->from('book'); 
 
    $this->db->where($condition); 
 
    $query = $this->db->get(); 
 
    return $query;

但經過當我點擊某個類別時,我會這樣做只是我點擊過的那個類別的書。有人可以幫助我選擇該類別的書嗎?

回答

0
public function category_details($id){ 
    $data = array(); 
    $data['category_detail'] = ""; 
    $query = $this->db->get_where('book', array('id' => $id)); 
    // Your current model logic will not work for this 
    if($query->num_rows() > 0){ 
     $data['category_detail'] = $query->result(); 
    } 
    $this->load->view('header', $data); 
    $this->load->view('category_details', $data); 
} 
+0

OIt說undefined變量id – Daniel

+0

它應該在哪裏需要這個變量$ id? – Daniel

相關問題