【问题标题】:where clause not working in codigniterwhere 子句在 codeigniter 中不起作用
【发布时间】:2016-11-13 11:20:54
【问题描述】:

我在带有 where 子句的页面中显示产品。我正在从类别为蔬菜的产品表中选择所有内容,但它不起作用。我是 CodeIgniter 的新手。我也在网上查了很多问题,但没有任何效果。如果这段代码有什么不对的地方,请告诉我。

控制器部分:

<?php
class products_list extends CI_Controller
{
function __construct() {
        parent::__construct();
        $this->load->helper('url');     //to load css base url
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->model("pagination_model");
        $this->load->library("pagination");
        $this->load->library('table');
}


function vegetables(){

    $this ->load->view('includes/header1');

    $config['base_url']='http://localhost/mah/index.php/products_list/vegetables';

    $vegetable="Vegetable";
    $config['total_rows']= $this->db->get_where('product', $vegetable)->num_rows();
    $config['per_page'] = 12;
    $config['num_links'] = 20;
    $config['full_tag_open']='<div id="pagination">';
    $config['full_tag_close']='</div>';

    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["results"] = $this->pagination_model->
            fetch_product($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();
    $this->load->view("product_display",    $data);

    $this ->load->view('includes/footer');

}

public function article($id)
{
    $this ->load->view('includes/header');
    $this->load->model('articles_model', 'product');
    $article = $this->product->find($id);
    $this->load->view('product_details', compact('article'));
}

}
?>

模型部分:

<?php
class pagination_model extends CI_Model
{
    public function __construct() {
        parent::__construct();
    }

    public function record_count() {
        $where="category='vegetable'";
        return $this->db->count_all("product");
    }

    public function fetch_product($limit, $start) {
        $this->db->limit($limit, $start);

        $query = $this->db->get("product");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
}
?>

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    试试这个...

    $config['total_rows']= $this->db->get_where('product', array('title' => $vegetable))
    ->num_rows();
    

    【讨论】:

    • 我认为您需要使用category 列而不是title
    【解决方案2】:

    控制器方法:

    根据您的要求和 base_url 剪裁链接

       public function vegetables() {
        $data = array('title' => 'Products');
        $config = array();
        $config['base_url'] = base_url().'products_list/vegetables';
        $config['total_rows'] = $this->pagination_model->countRows('products');
        $config['per_page'] = 5;
    
        $config['attributes'] = array('class' =>'item');
    
        $config['first_link'] = 'First';
        $config['cur_tag_open'] = '<a class="active item">';
        $config['cur_tag_close'] = '</a>';
    
        $config['next_link'] = 'Next';
        $config['last_link'] = 'Last';
        $config['prev_link'] = 'Previous';
    
    
        $this->pagination->initialize($config);
    
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["products"] = $this->pagination_model->getProducts('',$config["per_page"],$page);
        $data["categoriesData"] = $this->pagination_model->getProducts('vegetable',$config["per_page"],$page);
        $data["links"] = $this->pagination->create_links();
        print_r($data);die;
        //$this->load->view('templates/header',$data);
        $this->load->view('product_display',$data);
        //$this->load->view('templates/footer');    
    }
    

    你的模特:

    getProducts 获取两种类型的数据

    1:如果您提供类别作为第一个参数,则返回类别数据

    2:否则获取整个表;

       public function getProducts($category = NULL, $limit = NULL, $start = NULL)
      {
        $limit = $limit == NULL ? '' : $limit;
        $start = $start == NULL ? '' : $start;
        $this->db->order_by('id', 'DESC');
        $this->db->limit($limit, $start);
        if (empty($category)) 
        {
             $query = $this->db->get('products');
        }
        else 
        {
            $query = $this->db->get_where('products', array('category' => $category));
        }
    
    
        if ($query->num_rows() > 0) 
        {
            foreach ($query->result_array() as $row) 
            {
                $data[] = $row;
            }
            return $data;
        }
    }
     // total count method 
    
     public function countRows($table) {
        if (empty($table)) return false;
         return $this->db->count_all($table);
    }
    

    最后但并非最不重要:

    在控制器 __contruct() 方法中添加这个

    public function __construct()
     {
        parent::__construct();
        $this->load->helper(array('url','text','html','form'));
        $this->load->library(array('session','form_validation','pagination'));
        $this->load->database();
        $this->load->model('YourModel');
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-26
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 2014-03-20
      • 2018-01-21
      • 1970-01-01
      相关资源
      最近更新 更多