codeigniter模型功能和查询不起作用

问题描述 投票:0回答:3

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);

取消输出
根据table0的ID选择值1和value2。 如何解决这个问题,请帮助。

在控制器中使用此功能

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); } }

codeigniter mysqli
3个回答
1
投票

希望这会帮助您;

在此处以数组名称ID中的数组名称中获取所有内容,然后在循环外使用

0
投票

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()

您的控制器应将有效载荷从模型传递到视图。 public function sceneGrouper(array $qCodes): array { $result = $this->db ->select("t0.id AS sceneid, t0.value1 AS title, t1.id AS child_id, t1.value1 AS menu_scene, t1.value2 AS menu_item") ->from("table0 t0") ->join("table1 t1", "t1.id = t0.id", "left") ->where_in("t0.q_id", $qCodes) ->get() ->result(); $grouped = []; foreach ($result as $row) { $sceneId = $row->sceneId; $grouped[$sceneId] ??= (object) [ 'sceneId' => $sceneId, 'title' => $row->title, 'children' => [] ]; if ($row->child_id) { // don't bother adding empty children $grouped[$sceneId]->children[] = (object) [ 'id' => $row->child_id, 'menuScene' => $row->menu_scene, 'menuItem' => $row->menu_item ]; } } return array_values($grouped); }

在您的视图中,循环浏览分组数据。将所有内联样式移动到外部样式表,以使您的视图更可读性更易于管理。

0
投票


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.