如果我认为你正在使用 Codeigniter 4。如果这是真的。请在下面使用它。我认为它会帮助你解决这个问题。
如果您要扩展到 Codeigniter 核心模型,则不需要 $this->db。只是
扩展到 Codeigniter 核心模型,使用
public function products_count($category_id, $subcategory_id = null, $brand_id = null)
{
$query = $this->table('products'); // self::table('products')
if ($category_id) {
$query = $query->where('category_id', $category_id);
}
if ($subcategory_id) {
$query = $query->where('subcategory_id', $subcategory_id);
}
if ($brand_id) {
$query = $query->where('brand', $brand_id);
}
$query = $query->where('hide_pos !=', 1);
return $query->countAllResults();
}
如果不扩展到 Codeigniter Core 模型,请使用
public function __construct()
{
$this->db = db_connect();
}
public function products_count($category_id, $subcategory_id = null, $brand_id = null)
{
$query = $this->db->table('products');
if ($category_id) {
$query = $query->where('category_id', $category_id);
}
if ($subcategory_id) {
$query = $query->where('subcategory_id', $subcategory_id);
}
if ($brand_id) {
$query = $query->where('brand', $brand_id);
}
$query = $query->where('hide_pos !=', 1);
return $query->countAllResults();
}
如果您按照您所说的使用 codeigniter 3,请使用此
public function products_count($category_id, $subcategory_id = null, $brand_id = null)
{
$query = $this->db->from('products');
if ($category_id) {
$query = $query->where('category_id', $category_id);
}
if ($subcategory_id) {
$query = $query->where('subcategory_id', $subcategory_id);
}
if ($brand_id) {
$query = $query->where('brand', $brand_id);
}
$query = $query->where('hide_pos !=', 1);
return $query->count_all_results();
}
或
public function products_count($category_id, $subcategory_id = null, $brand_id = null)
{
$query = $this->db->from('products');
if ($category_id) {
$query = $query->where('category_id', $category_id);
}
if ($subcategory_id) {
$query = $query->where('subcategory_id', $subcategory_id);
}
if ($brand_id) {
$query = $query->where('brand', $brand_id);
}
$query = $query->where('hide_pos !=', 1)->get();
return $query->num_row();
}
如果不引起我的注意,我希望这会有所帮助