我正在尝试创建一个目录并且在调用"列出图像时遇到问题"在结果中。问题是只有一些列表会有图像,否则如果它们没有,我希望它们使用我设置的默认图像。当我尝试添加'图像'表格到我的查询,它只返回有图像可用的结果(忽略其他没有图像的列表)。
这是我的代码:
public function search($neighborhood = null, $biz_filter = null) {
$neighborhood = $this->uri->segment(3);
$biz_filter = $this->uri->segment(4);
// SELECT
$this->db->select('*');
// MAIN TABLE TO GRAB DATA
$this->db->from('biz');
// TABLES TO JOIN
$this->db->join('city', 'city.city_id = biz.biz_cityID');
$this->db->join('zip', 'zip.zip_id = biz.biz_zipID', 'zip.zip_cityID = city.city_id');
$this->db->join('state', 'state.state_id = city.city_stateID');
$this->db->join('neighborhood', 'neighborhood.neighborhood_id = biz.biz_neighborhoodID');
$this->db->join('biz_filter', 'biz_filter.bizfilter_bizID = biz.biz_id');
$this->db->join('biz_category', 'biz_category.bizcategory_id = biz_filter.bizfilter_bizcategoryID');
if ($neighborhood != "-" AND $biz_filter != "-") {
$this->db->where('biz_category.bizcategory_slug', $biz_filter);
$this->db->where('neighborhood.neighborhood_slug', $neighborhood);
} elseif ($neighborhood != "-" AND $biz_filter == "-") {
$this->db->where('neighborhood.neighborhood_slug', $neighborhood);
} elseif ($neighborhood == "-" AND $biz_filter != "-") {
$this->db->where('biz_category.bizcategory_slug', $biz_filter);
} else {
}
// ORDER OF THE RESULTS
$this->db->group_by('biz_name asc');
// RUN QUERY
$query = $this->db->get();
// IF MORE THAN 0 ROWS ELSE DISPLAY 404 ERROR PAGE
return $query;
}
如何添加单独的表格,' image'它包含徽标图像(' image.image_file')。 '图像'桌子和' biz'表通过每个表传递的业务ID连接(image.biz_id = biz.biz_id)。
任何人都知道如何解决查询才能正常工作?
答案 0 :(得分:0)
只需使用
$this->db->join('image', 'image.biz_id = biz.biz_id', 'left');
离开加入您的image
表格。如果biz_id表中没有记录,image.image_file
将具有空值。请阅读here以获取更多信息。
您可以使用COALESCE函数替换" null"具有预定义默认值的图像。只需将您的行$this->db->select('*');
替换为此行:
// SELECT
$this->db->select("*, COALESCE(image.image_file, 'images/not_found.png') as my_image_file");
渲染输出时,请确保使用my_image_file
列作为图像。
旁注:避免使用' *'在选择中。仅选择您实际需要的列。选择所有列会不必要地增加数据库服务器资源的负载。