我正在使用CodeIgniter Framework,我有两个li标签,我想用第一个表的所有数据填充第一个li,并且根据id,如果从第一个表和第二个表匹配,我有另一个选择查询获取数据并在第二个li标签中显示它。这是核心php中的工作代码,但不能在CodeIgniter中运行,但问题是我只能从一个表中获取数据并将其传递给控制器查看。请帮助我如何在视图中使用两个表数据。
<?php
$sql = 'SELECT * FROM course_sections ORDER BY item_order ASC';
$query = $pdo->prepare($sql);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) { ?>
<li style="sample_style"> <?=$rs['title'];?> </li>
<?php
$sql = 'SELECT * FROM course where itemid = :pid ORDER BY item_order ASC';
$query = $pdo->prepare($sql);
$query->bindParam(':pid', $rs['id'], PDO::PARAM_INT);
$query->execute();
$f_list = $query->fetchAll();
foreach ($f_list as $get_final_list) { ?>
<li style="sample_style"> <?=$get_final_list['title'];?> </li>
<? } } ?>
CodeIgniter代码:
模特功能:
function get_all_topics()
{
$this->load->database();
$this->db->select("*");
$this->db->from("course_sections");
$this->db->order_by("item_order", "ASC");
$query=$this->db->get();
return $query->result();
}
控制器功能:
function index()
{
$this->load->model('drag_drop_model');
$data['query']=$this->drag_drop_model->get_all_topics();
$this->load->helper('url');
$this->load->view('drag_drop_course_material', $data);
}
答案 0 :(得分:0)
试试这段代码,
您的型号代码: -
function get_all_topics()
{
$this->load->database();
$this->db->select("cs.title,group_concat(c.title) as course_title");
$this->db->from("course_sections cs");
$this->db->join("course c","c.id=cs.itemid","left");
$this->db->order_by("cs.item_order", "ASC");
$this->db->group_by("c.id");
$query=$this->db->get();
return $query->result();
}
在你的竞争中: -
foreach ($list as $rs) { ?>
<li style="sample_style"> <?=$rs['title'];?> </li>
$f_list = explode(",",$rs['course_title']);
foreach ($f_list as $get_final_list) { ?>
<li style="sample_style"> <?=$get_final_list;?> </li>
<? } } ?>