2017-01-20 72 views
1

我創建了一個顯示來自CodeIgniter中數據庫的衣物的小項目。數據來自數據庫,它的工作原理和我已經把條件檢查類別和條件不起作用。它不顯示特定的結果,而是顯示所有數據庫表結果。我通過url傳遞類別參數。我已經自動加載數據庫。通過CodeIgniter中的控制器將參數傳遞給模型

我這是怎麼通過在主頁的參數,當用戶單擊該男子鏈接

<a class="dropdown-item" href="<?php echo base_url();?>index.php/Products/view/mens">Men's</a> 

這是控制器

<?php 
if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Products extends CI_Controller { 
    public function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('GetData'); 
} 


    public function view() 
    { 
     $category_name = $this->uri->segment(3); 
     $this->GetData->get_data($category_name); 
     $result = $this->GetData->get_data(); 
     $data['result'] = $result; 
     $this->load->view('products',$data); 
    } 

} 

/* End of file Products.php */ 
/* Location: ./application/controllers/Products.php */ 

?> 

這是模型

<?php 
if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class GetData extends CI_Model { 


    public function __construct() 
    { 

     parent::__construct(); 

    } 

    public function get_data($category_name=0){ 
     $query = $this->db->get_where('mens', array('category' => $category_name)); 
     return $query->result(); 
    } 


} 

/* End of file GetData.php */ 
/* Location: ./application/models/GetData.php */ 
?> 

數據庫表字段 - id,類別,category_name,img_path,item_name

我用where條件來檢查category是否等於mens,如果是顯示結果..並且表中有category = women的一行。但不是顯示特定的結果,而是顯示所有結果。

+0

爲什麼你在模型中傳遞$ categoryname = 0?刪除= 0並檢查。 –

+0

@jyotimishra:這不是問題,他試圖獲取數據,其中$ category_name = 0; –

+0

@jyotimishra我已經嘗試過,當我刪除,我得到這2個錯誤.. – Kasun

回答

1

型號:

public function get_data($category_name){ 
     $query = $this->db->get_where('mens', array('category' => $category_name)); 
     return $query->result(); 
    } 

控制器:

public function view() 
    { 
     $category_name = $this->uri->segment(3);   
     $result = $this->GetData->get_data($category_name); 
     $data['result'] = $result; 
     $this->load->view('products',$data); 
    } 

檢查。

請檢查$ category_name的第一個值是否存在。

//刪除$this->GetData->get_data($category_name);從控制器,因爲你已經再次定義這一點。不明白爲什麼你要在控制器中調用兩次相同的函數。

+0

感謝它的工作,你救了我的一天! – Kasun

相關問題