如何在 Codeigniter 的视图中使用两个表数据
How to use two tables data in a view in Codeigniter
我有两个 table,第一个 table 是主题名称,第二个 table 显示子主题。我想从第一个 table 主题名称开始显示,然后搜索相应的子主题并将其显示在视图中,依此类推,直到在第一个 table 中找到主题名称。但问题是我只能看到一个 table 数据,即主要主题 table。我不知道如何获取 table 的完整数据并使用它。
Database Structure:
Model:
<?php
class Topics_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
function get_all_topics()
{
$this->load->database();
$this->db->select("*");
$this->db->from("topics");
$query=$this->db->get();
return $query->result();
}
}
?>
Controller :
<?php
class drag_drop_course_material extends CI_Controller {
function drag_drop_course_material()
{
parent::__construct();
}
function index()
{
$this->load->model('topics_model');
$data['query']=$this->topics_model->get_all_topics();
$this->load->helper('url');
$this->load->view('drag_drop_course_material', $data);
}
}
?>
查看:
<?php
foreach ($query as $row) {
?>
<li> $row->topic_name </li>
<? } ?>
所需输出:
试试这个查询。我们对两个表进行左连接。
$CI->db->select('*');
$CI->db->from('topic');
$CI->db->join('sub-topics', 'topic.id = sub-topics.sub_topic_id', 'left');
$query = $CI->db->get();
$result = $query->result();
您将在您的模型中得到结果。 return 这个结果来自模型并在控制器中访问它。一旦你在控制器中打印 return 结果,你就会知道如何渲染它,因为你已经在上面做了。
你的控制器是完美的,你的模型是完美的只是改变你的看法:
在视图中:
foreach ($query as $row)
{ ?>
<ul>
<li><?php echo $row['topic_name'];?>
<ul>
<?php $this->db->where('sub_topic_id',$row['id'])
$r = $this->db->get('subtopics');
if($r->num_rows()>0)
{
foreach ($r -> result_array() as $row) {
$data1[] = $row;
}
}
foreach($data1 as $sub){?>
//print <li><?php echo $sub['subtopic_name']?></li>
<? }?>
</ul>
</li>
<? }?>
</ul>
我有两个 table,第一个 table 是主题名称,第二个 table 显示子主题。我想从第一个 table 主题名称开始显示,然后搜索相应的子主题并将其显示在视图中,依此类推,直到在第一个 table 中找到主题名称。但问题是我只能看到一个 table 数据,即主要主题 table。我不知道如何获取 table 的完整数据并使用它。
Database Structure:
Model:
<?php
class Topics_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
function get_all_topics()
{
$this->load->database();
$this->db->select("*");
$this->db->from("topics");
$query=$this->db->get();
return $query->result();
}
}
?>
Controller :
<?php
class drag_drop_course_material extends CI_Controller {
function drag_drop_course_material()
{
parent::__construct();
}
function index()
{
$this->load->model('topics_model');
$data['query']=$this->topics_model->get_all_topics();
$this->load->helper('url');
$this->load->view('drag_drop_course_material', $data);
}
}
?>
查看:
<?php
foreach ($query as $row) {
?>
<li> $row->topic_name </li>
<? } ?>
所需输出:
试试这个查询。我们对两个表进行左连接。
$CI->db->select('*');
$CI->db->from('topic');
$CI->db->join('sub-topics', 'topic.id = sub-topics.sub_topic_id', 'left');
$query = $CI->db->get();
$result = $query->result();
您将在您的模型中得到结果。 return 这个结果来自模型并在控制器中访问它。一旦你在控制器中打印 return 结果,你就会知道如何渲染它,因为你已经在上面做了。
你的控制器是完美的,你的模型是完美的只是改变你的看法:
在视图中:
foreach ($query as $row)
{ ?>
<ul>
<li><?php echo $row['topic_name'];?>
<ul>
<?php $this->db->where('sub_topic_id',$row['id'])
$r = $this->db->get('subtopics');
if($r->num_rows()>0)
{
foreach ($r -> result_array() as $row) {
$data1[] = $row;
}
}
foreach($data1 as $sub){?>
//print <li><?php echo $sub['subtopic_name']?></li>
<? }?>
</ul>
</li>
<? }?>
</ul>