Codeigniter连接不返回数据库结果
Codeigniter不支持数据库结果。 数据库表“类别”和“子类别” 谢马:Codeigniter连接不返回数据库结果,codeigniter,Codeigniter,Codeigniter不支持数据库结果。 数据库表“类别”和“子类别” 谢马: Categor ----------------------------- ID Name ---------------------------- 1 Fishing 2 Hunting 3 Test Category Sub_category ----------------------------- ID cat_id name -----------
Categor
-----------------------------
ID Name
----------------------------
1 Fishing
2 Hunting
3 Test Category
Sub_category
-----------------------------
ID cat_id name
----------------------------
1 1 Fishing rod
2 2 Hunting ammunition
3 3 Test sub category
我想列出一些类别的所有子类别。当有人点击钓鱼类别时,我想显示钓鱼的所有子类别。我的代码是:
Controller:
public function get_sub_category($id = 0)
{
$this->load->model('front_m');
$data['sub_cat'] = $this->front_m->show_sub_cat($id);
$this->template->set_theme('zend')->set_layout('front.html')
->build('sub_category',$data);
}
MODEL:
public function show_sub_cat($id=0)
{
$this->query = $this->db->select('*');
$this->query = $this->db->from('category');
$this->query = $this->db->where('id='.$id');
$this->query = $this->db->join('sub_category', 'sub_category.cat_id=category.id');
$this->query = $this->db->query('SELECT * FROM category');
$this->query = $this->db->get();
if ($this->query->num_rows() > 0) {
$this->query->result();
}
return $this->query ;
}
我一直都有DB错误或空白页。根据你的问题,听起来像是你想得太多了,但我也有点困惑。我听到的是:如何根据某人单击主类别id来获取我的子类别?如果是这样的话,那你就让事情变得比需要的更复杂 模型
public function show_sub_cat($catid=NULL)
{
$result = $this->db->get_where('sub_category',array('cat_id'=>$catid));
if ($result->num_rows()>0)
{
return $result->result();
}
}
$sql_query ="SELECT
c.name,
sc.*
FROM Category as c
LEFT JOIN Sub_category as sc ON sc.cat_id = c.ID
WHERE c.ID = $id";
return $this->db->query($sql_query)->result();