【问题标题】:passing parameter to model via controller in CodeIgniter通过 CodeIgniter 中的控制器将参数传递给模型
【发布时间】:2017-01-20 09:27:57
【问题描述】:

我创建了一个小项目,用于显示来自 CodeIgniter 数据库的服装项目。数据来自数据库并且它正在工作,我已经设置了一个 where 条件来检查类别和 where 条件不起作用。它不显示特定结果,而是显示所有数据库表结果。我通过 url 传递类别参数。我已经自动加载了数据库。

这是我在用户点击男士链接时在主页中传递参数的方式

<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,category_name,img_path,item_name

我使用 where 条件来检查类别是否等于男装,如果是则显示结果..并且表格中有一行类别 = 女装。但不是显示特定结果,而是显示所有结果。

【问题讨论】:

  • 为什么要在模型中传递 $categoryname = 0?删除 =0 并检查。
  • @jyotimishra:这不是问题,他正在尝试获取 $category_name = 0 的数据;
  • @jyotimishra 我已经尝试过了,当我删除它时,我得到了这两个错误..
  • 我得到这 2 个错误 Missing argument 1 for GetData::get_data()Undefined variable: category_name 当我删除它时

标签: php mysql codeigniter


【解决方案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-&gt;GetData-&gt;get_data($category_name);来自控制器,因为您已经再次定义了它。不明白为什么您在控制器中两次调用相同的函数。

【讨论】:

    猜你喜欢
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多