IAM尝试在Codeigniter模型中运行查询。它可以正常工作,但是当我回荡模型函数时,查询如下。
SELECT * FROM `table1` WHERE `id` = '17'
SELECT * FROM `table1` WHERE `id` = '20'
SELECT * FROM `table1` WHERE `id` = '21'
SELECT * FROM `table1` WHERE `id` = '22'
SELECT * FROM `table1` WHERE `id` = '23'
我的模型功能在下面给出
function get_quick_navi_menu($q_code)
{
$this->db->select("*");
$this->db->where('q_id',$q_code);
$this->db->from("table0");
$q = $this->db->get();
//echo $this->db->last_query();
$final = array();
if ($q->num_rows() > 0)
{
foreach ($q->result() as $row) {
$this->db->select("*");
$this->db->from("table1");
$this->db->where("id",$row->id);
$q = $this->db->get();
echo $this->db->last_query();
if ($q->num_rows() > 0) {
$row->children = $q->result();
}
array_push($final, $row);
}
}
SELECT * FROM `table1` WHERE `id` = '17,18,19..'
Table0
id q_id value1
1 2 4
2 2 5
3 2 6
Table1
t1_id id value1 value2
1 1 2 2
2 2 5 6
3 3 8 12
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1" style="margin-left: 1px; opacity: .9;">
<?php foreach ($menus as $menu) { ?>
<li class="dropdown-submenu"><a href="#" class="pan-btn" data-look="<?php echo $menu->sceneid;?>"><?php echo $menu->title;?></a>
<ul class="dropdown-menu"> <?php
if (isset($menu->children)) {
foreach ($menu->children as $child) {?>
<li><a href="#" class="pan-btn" data-look="<?php echo $child->menu_scene;?>"><?php echo $child->menu_item;?></a></li> <?php
}
}
?></ul></li><?php } ?>
</ul>
$menus = $this->Home_model->get_quick_navi_menu($q_code);
$data = array('menus' => $menus);
取消输出
在控制器中使用此功能
public function getTableData()
{
$this->db->select('GROUP_CONCAT(id) as id');
$tbl0 = $this->db->get('table0')->row_array();
if($tbl0) {
$ids = explode(',', $tbl0['id']);
$this->db->where_in('id', $ids);
$tbl1 = $this->db->get('table1')->result_array();
echo "<pre>"; print_r($tbl1);
}
}
希望这会帮助您;
在此处以数组名称ID中的数组名称中获取所有内容,然后在循环外使用
where_in
在您的控制器类中:
确保您已将模型和数据库加载到控制器或autoLoad.php
中
public function get_quick_navi_menu($q_code)
{
$this->db->select("*");
$this->db->where('q_id',$q_code);
$this->db->from("table0");
$q = $this->db->get();
$final = array();
if ($q->num_rows() > 0)
{
foreach ($q->result() as $key => $row)
{
$ids[$key] = $row->id;
$data[$key] = $row;
}
$this->db->select("*");
$this->db->from("table1");
$this->db->where_in("id",$ids);
$q = $this->db->get();
//echo $this->db->last_query();
if ($q->num_rows() > 0)
{
if ( ! empty($data))
{
foreach ($data as $key => $item)
{
$item->children = $q->result();
$final[] = $item;
}
}
}
}
return $final;
/*print_r($final);*/
}
在您的观点中:
$q_code = 'q_code_value';
$data['menus'] =$this->Home_model->get_quick_navi_menu($q_code);
/* pass the $data in the view like this*/
$this->load->view('your_view_file_path',$data);
更多:Https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-for-pecific-data
I建议在数据库总数上仅执行一个查询,然后使用PHP来构建多维数组。 使用
<div><?php print_r($records);?></div>
将ID数组传递给您的Where子句。 使用左连接将确保您的数组中即使是“无儿童”场景。 定期的联接(内部联接)将排除第二个没有孩子的场景。
where_in()
在您的视图中,循环浏览分组数据。将所有内联样式移动到外部样式表,以使您的视图更可读性更易于管理。