2017-01-20 160 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

相关问题