我正在 condeigniter(新手)中从事一个项目。我的项目包括使用不同过滤器(如类别、子类别、关键字、最低-最高价格等)列出产品。我已经在自定义 php 中执行了数百次这样的操作,其中我为所有条件创建变量 $where
where
子句。例如
$where = '';
if($category != '')
{
$where .= " category = '$category'";
}
现在在codeigniter中我有多个参数,我可以通过uri段
进入我的 Controller
并调用model
函数,传递所有参数并在那里运行 mysql 查询
。问题是我正在检查每个参数是否为空或设置并相应地运行多个查询。例如。
if($category != '' && $subCategory != '')
{
//whole query here
}
else if($category == '' && $subCategory != '')
{
//whole query here
}
//and so on
我需要的是任何优化方法来执行此操作,因为我有连接(多个参数)。
我的示例条件和查询
if($subCat == '' && $vendor == '')
{
$cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
$this->db->select('listings.* ');
$this->db->from('listings');
$this->db->join('categories','categories.id = listings.category');
$this->db->where('categories.name',$cat);
$this->db->limit($perpage,$page);
$query = $this->db->get();
return $query->result();
}
else if($subCat == '' && $vendor != '')
{
$cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
$this->db->select('listings.* ');
$this->db->from('listings');
$this->db->join('categories','categories.id = listings.category');
$this->db->join('vendors','vendors.id = listings.programme');
$this->db->where('categories.name',$cat);
$this->db->where('vendors.name',$vendor);
$this->db->limit($perpage,$page);
$query = $this->db->get();
return $query->result();
}
else if($subCat != '' && $vendor == '')
{
$cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
$this->db->select('listings.* ');
$this->db->from('listings');
$this->db->join('categories','categories.id = listings.category');
$this->db->join('subcategories','subcategories.id = listings.subcategory');
$this->db->where('categories.name',$cat);
$this->db->where('subcategories.name',$subCat);
$this->db->limit($perpage,$page);
$query = $this->db->get();
return $query->result();
}
else
{
$cat = str_replace("-"," ",str_replace("-and-"," & ",$cat));
$subCat = str_replace("-"," ",str_replace("-and-"," & ",$subCat));
$vendor = str_replace("-"," ",str_replace("-and-"," & ",$vendor));
$this->db->select('listings.* ');
$this->db->from('listings');
$this->db->join('categories','categories.id = listings.category');
$this->db->join('subcategories','subcategories.id = listings.subcategory');
$this->db->join('vendors','vendors.id = listings.programme');
$this->db->where('categories.name',$cat);
$this->db->where('subcategories.name',$subCat);
$this->db->where('vendors.name',$vendor);
$this->db->limit($perpage,$page);
$query = $this->db->get();
return $query->result();
}
最佳答案
您只需编写一次查询,而不是每次都编写。例如,您可能有您所描述的几种情况。例如,这是我所做的事情:
$this->db->select('resource.id,city.name as city_name,city.provinceID,resource.Rate, skills.name')->from('resource')
->join('city','resource.cityID = city.id')
->join('resourceToSkillsMap','resource.id = resourceToSkillsMap.resourceID')
->join('skills','skills.id = resourceToSkillsMap.skillsID');
if($category != '' && $subCategory != '') #condition
{
$this->db->where('condition');
}
else if($category == '' && $subCategory != '') #condition
{
$this->db->where('condition');
}
$rs = $this->db->group_by('resource.id')
->limit($limit,$offset)
->get();
查看查询如何仅编写一次
,但where 条件
在if else
内给出。所以你的查询将以这种方式建立。执行查询后,只需 echo $this->db->last_query();
即可回显查询并根据需要进行调整。
关于php - 将参数从 Controller 传递到模型以进行 mysql 查询的优化方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18843186/