gpt4 book ai didi

php - 根据类别和颜色搜索结果

转载 作者:行者123 更新时间:2023-11-29 13:34:16 26 4
gpt4 key购买 nike

我有4个mysql表如下..

产品(id、sku、名称、color_id、描述、摘录)

颜色(颜色名称、颜色 ID)

类别(categ_name、categ_id)

产品类别(pid、cid)

现在我要执行搜索操作。目前我的搜索查询基于产品 id、sku、描述或 color_id。

我的查询在 codeigniter 中如下......

$color_id   =   0;
$this->db->select('color_id');
$this->db->where('LOWER(color_name)',strtolower($term));
$color_row = $this->db->get('product_colors')->row();
if($color_row)
$color_id = $color_row->color_id;

$this->db->select('*, LEAST(IFNULL(NULLIF(saleprice, 0), price), price) as sort_price', false);
//this one gets just the ones we need.
$this->db->where('enabled', 1);
$this->db->where('(name LIKE "%'.$term.'%" OR description LIKE "%'.$term.'%" OR excerpt LIKE "%'.$term.'%" OR sku LIKE "%'.$term.'%" OR color='.$color_id.')');

这工作得很好。但我也想基于类别进行搜索。例如,我想搜索“Red Shoes”,其中红色是颜色,鞋子是类别名称。

请告诉我如何为此构建查询。

这将是一个很大的帮助。

谢谢!!!

最佳答案

加入所有 table 来尝试这个

$term=$_POST['your search field name'];
$keyword=explode(" ",$term);
$this->db->select('p.*');
$this->db->from('Products p');
$this->db->join('Product_Categories pc', 'p.id = pc.pid','LEFT');
$this->db->join('Categories c', 'pc.cid = c.categ_id','LEFT');
$this->db->join('Colors co', 'p.color_id = co.color_id','LEFT');
$this->db->where('p.enabled', 1);

foreach($keyword as $k){
$this->db->or_where('p.id =', $k);//or_where
$this->db->like('p.sku', $k);
$this->db->like('p.description', $k);//or_like
$this->db->like('co.color_name', $k);
$this->db->like('c.categ_name', $k);

}

$this->db->group_by("p.id");

Active Record Class

关于php - 根据类别和颜色搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18957867/

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