gpt4 book ai didi

php - where 子句在 codeigniter 中不起作用

转载 作者:行者123 更新时间:2023-11-29 20:47:51 27 4
gpt4 key购买 nike

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

my table

Controller 部分:

<?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;
}
}
?>

最佳答案

Controller 方法:

根据您的要求和 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);
}

最后但并非最不重要的一点:

将其添加到 Controller __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');
}

关于php - where 子句在 codeigniter 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38308166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com