包含子类别PHP/MySQL的类别
我有MySql表名“category”,在该表中我有包含子类别PHP/MySQL的类别,php,mysql,html,codeigniter,html-lists,Php,Mysql,Html,Codeigniter,Html Lists,我有MySql表名“category”,在该表中我有 id catname parent_id 1 animals 2 vegs 3 dog 1 4 cat 1 5 carrot 2 我只想用html嵌套的'ul'显示这些数据,就像 <ul> <li>Animals <ul> <li>dog</li>
id catname parent_id
1 animals
2 vegs
3 dog 1
4 cat 1
5 carrot 2
我只想用html嵌套的'ul'显示这些数据,就像
<ul>
<li>Animals
<ul>
<li>dog</li>
<li>cat</li>
</ul>
</li>
<li>Vegs
<ul>
<li>Carrot</li>
</ul>
</li>
</ul>
- 动物
- 狗
- 猫
- 蔬菜
- 胡萝卜
使用php,请帮助我使用php(CodeIgniter)获取这些数据并进行显示。以下是三个组件:控制器、模型和视图
<!-- THE VIEW -->
<ul>
<?php foreach($main_cat->result() as $rowm) : ?>
<li><?php echo $rowm->catname ?>
<ul>
<?php
$id = $rowm->id;
$sec_cat = $this->yourmodel->get_secondary($id);
foreach($sec_cat->result() as $rows) :
?>
<li><?php echo $rows->catname ?></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
<!-- THE CONTROLLER -->
<?php
class Welcome extends CI_Controller {
function index()
{
$data['main_cat'] = $this->yourmodel->get_main();
$this->load->view('welcome_view',$data);
}
}
?>
<!-- THE MODEL -->
<?php
class Yourmodel extends CI_Model {
function get_main()
{
$this->db->where('parent_id','');
$query = $this->db->get('category');
return $query;
}
function get_secondary($parent)
{
$this->db->where('parent_id',$parent);
$query = $this->db->get('category');
return $query;
}
}
?>
-
以下是您的问题的三个组成部分:控制器、模型和视图
<!-- THE VIEW -->
<ul>
<?php foreach($main_cat->result() as $rowm) : ?>
<li><?php echo $rowm->catname ?>
<ul>
<?php
$id = $rowm->id;
$sec_cat = $this->yourmodel->get_secondary($id);
foreach($sec_cat->result() as $rows) :
?>
<li><?php echo $rows->catname ?></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
<!-- THE CONTROLLER -->
<?php
class Welcome extends CI_Controller {
function index()
{
$data['main_cat'] = $this->yourmodel->get_main();
$this->load->view('welcome_view',$data);
}
}
?>
<!-- THE MODEL -->
<?php
class Yourmodel extends CI_Model {
function get_main()
{
$this->db->where('parent_id','');
$query = $this->db->get('category');
return $query;
}
function get_secondary($parent)
{
$this->db->where('parent_id',$parent);
$query = $this->db->get('category');
return $query;
}
}
?>
-
这个问题在这里已经提过好几次了。请使用本网站的“搜索”功能。例如:-并查看本网站右侧的“相关”问题,以获得许多类似的问题/答案。此问题已在此处多次提出。请使用本网站的“搜索”功能。例如:-并查看本网站右侧的“相关”问题,以获得大量类似的问题/答案。