我在带有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;
}
}
?>
答案 0 :(得分:0)
试试这个...
$config['total_rows']= $this->db->get_where('product', array('title' => $vegetable))
->num_rows();
答案 1 :(得分:0)
控制器方法:
根据您的要求和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');
}