我正在尝试使用PHP Codeigniter创建动态菜单。
有一些主菜单 每个主菜单都有一些Sub_Menu Like,Categories - > Hardware,Software等。
我以某种方式制作但有问题,主菜单显示正确,但主菜单下的子菜单在每个主菜单中重复。
这是问题的形象:
我的观看代码
<ul class="slimmenu">
<?php foreach($menus as $menu): ?>
<li>
<a href="#"><?php echo $menu -> Menu_Name;?></a>
<ul>
<?php foreach($submenus as $submenu): ?>
<li>
<a href="#"><?php echo $submenu -> Sub_Menu_Name;?></a>
</li>
<?php endforeach;?>
</ul>
</li>
<?php endforeach;?>
</ul>
我的控制器代码
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MainSystem extends CI_Controller
{
function __construct() {
parent::__construct();
$this->load->model('menusys','ms');
}
function menu()
{
$this->data['menus'] = $this->ms->get_menu();
$this->data['submenus'] = $this->ms->get_submenu();
$this->load->view('index', $this->data);
}
function add_menu()
{
$data = array(
'Menu_Name' => $this->input->post('mname'),
'Menu_Link' => $this->input->post('mlink'),
);
$this->ms->adm($data);
}
function sub_menu()
{
$this->data['mmenu'] = $this->ms->get_menu();
$this->load->view('submenu', $this->data);
}
function add_submenu()
{
$data = array(
'Main_Menu_Name' => $this->input->post('mmname'),
'Sub_Menu_Name' => $this->input->post('mname'),
'Sub_Menu_Link' => $this->input->post('mlink'),
);
$this->ms->adsm($data);
}
}
?>
我的型号代码
<?php
defined('BASEPATH') or exit('no direct script directy allowed');
/**
*
*/
class menusys extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function adm($data)
{
$this->db->set('Is_Active',0);
$this->db->insert('main_menu',$data);
$this->session->set_flashdata('msg', 'Meun has been added');
redirect('MainSystem/menu');
}
//sub-menu Section
public function get_menu()
{
//$this->db->where('Is_Active',1);
// $this->db->select('*');
// $this->db->from('main_menu');
// $this->db->join('sub_menu', 'main_menu.Menu_Name = sub_menu.Main_Menu_Name','inner');
// $query = $this->db->get();
// return $query->result();
//
$query=$this->db->get_where('main_menu',array('Is_Active'=>1));
return $query->result();
}
function get_submenu()
{
$this->db->where('main_menu.Menu_Name = sub_menu.Main_Menu_Name');
$this->db->select('*');
$this->db->from('sub_menu');
$this->db->join('main_menu', 'main_menu.Menu_Name = sub_menu.Main_Menu_Name','inner');
$query = $this->db->get();
return $query->result();
}
public function adsm($data)
{
$this->db->set('Is_Active',1);
$this->db->insert('sub_menu',$data);
$this->session->set_flashdata('msg', 'Meun has been added');
redirect('MainSystem/menu');
}
}
我想要的是什么:我希望每个Sub_Menu都会显示在他的父菜单下。
任何帮助将不胜感激。
更新
这是子菜单表图像。
答案 0 :(得分:0)
您需要格式化菜单数组并将其循环播放
$menu = array(
'menu_name_1' => array(
'submenu1_1' => 'www.something.com',
'submenu1_2' => 'www.something.com',
),
'menu_name_2' => array(
'submenu2_1' => 'www.something.com',
'submenu2_2' => 'www.something.com',
)
);
foreach($menu as $menu_name => $submenu){
echo $menu_name.'<br>';
if (!empty($submenu)){
foreach($submenu as $submenu_name => $submenu_link){
echo '<a href="'.$submenu_link.'">'.$submenu_name.'</a><br>';
}
}
}
您有包含菜单名称和子菜单的数组。首先你循环菜单名称和检查剂量它有子菜单,如果有,你循环子菜单并建立链接。