CodeIgniter Forums

Full Version: problem: create dropdown with multidimensional array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys
I want to create a dropdwon with  multidimensional array
for example:
AAA
→BBB
→ →BBBsub1
→ →BBBsub2
→ →BBBsub3
→CCC


my database:
id       parent_id     name

-----------------------------------------
please help me
tnx
You can play around with this code, just pass it your array from your database.


PHP Code:
public function html_menu($menu$parent_id 0$parents = array())
{
 
       if ($parent_id == 0)
 
       {
 
           foreach ($menu as $item)
 
           {
 
               if (($item['parent_id'] != 0) && !in_array($item['parent_id'], $parents))
 
               {
 
                   $parents[] = $item['parent_id'];
 
               }
 
           }
 
       }

 
       $html '';

 
       foreach ($menu as $item)
 
       {
 
           if ($item['parent_id'] == $parent_id)
 
           {
 
               $html .= '<li><a href="'.$item['url'].'">'.$item['title'].'</a>';

 
               if (in_array($item['id'], $parents))
 
               {
 
                   $html .= '<ul>';
 
                   $html .= $this->html_menu($menu$item['id'], $parents);
 
                   $html .= '</ul>';
 
               }

 
               $html .= '</li>';
 
           }
 
       }

 
       $html .= '';

 
       return $html;
 
   

This is the database tables that I use for Bootstrap menus. It's just for test the menus.
Bootstrap will only allow one dropdown level, but I have the code to make it multi-level.

PHP Code:
--
-- 
Table structure for table `menu_group`
--

DROP TABLE IF EXISTS `menu_group`;
CREATE TABLE IF NOT EXISTS `menu_group` (
    `
id   int(11     unsigned NOT NULL AUTO_INCREMENT,
    `
titlevarchar(255         NOT NULL,
    
PRIMARY KEY (`id`)
ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- 
Dumping data for table `menu_group`
--

INSERT INTO `menu_group` (`id`, `title`) VALUES
    
(1'Main Menu'),
    (
2'Footer Menu'),
    (
3'Member Menu'),
    (
4'Admin Menu');


-- --------------------------------------------------------

--
-- 
Table structure for table `menu`
--

DROP TABLE IF EXISTS `menu`;
CREATE TABLE IF NOT EXISTS `menu` (
    `
id       int(11     unsigned NOT NULL AUTO_INCREMENT,
    `
parent_idint(11     unsigned NOT NULL DEFAULT 0,
    `
group_id int(11     unsigned NOT NULL DEFAULT 1,
    `
order    int(11     unsigned NOT NULL DEFAULT 0,
    `
title    varchar(100         NOT NULL DEFAULT '',
    `
url      varchar(255         NOT NULL DEFAULT '',
    `
icon     varchar(100         NOT NULL DEFAULT '',
    `
target   enum('none','_blank','_self','_parent','_top'NOT NULL DEFAULT 'none',
    
PRIMARY KEY (`id`)
ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- 
Dumping data for table `menu`
--

INSERT INTO `menu` (`id`, `parent_id`, `group_id`, `order`, `title`, `url`, `icon`, `target`) VALUES
    
(1 0 11'Home''/''''none'),
    (
2 0 12'About''/about.html''''none'),
    (
3 2 11'Company Profile''/company-profile.html''''none'),
    (
4 0 23'Affiliate''/affiliate.html''''none'),
    (
5 0 22'Forum''/forum''''none'),
    (
6 0 21'Make Money''/make-money.html''''none'),
    (
7 0 15'Contact Us''/contact-us.html''''none'),
    (
8 0 14'Blog''/blog''''none'),
    (
9 0 13'Products''/products''''none'),
    (
109 11'Handicraft''/products/handicraft''''none'),
    (
119 12'Furniture''/products/furniture''''none'),
    (
121011'Tissue Box''/products/handicraft/tissue''''none'),
    (
131012'Frame''/products/handicraft/frame''''none'),
    (
141111'Cabinet''/products/furniture/cabinet''''none'),
    (
151112'Chair''/products/furniture/chair''''none'),
    (
161113'Table''/products/furniture/table''''none'),
    (
170 24'Help''/help''''none'),
    (
182021'Support Center''/support-center.html''''none'),
    (
192021'Sitemap''/sitemap.html''''none'),
    (
200 31'Author Dashboard''/author-dashboard''''none'),
    (
210 32'My Profile''/member/profile''''none'),
    (
220 33'Settings''/member/settings''''none'),
    (
230 34'Downloads''/member/downloads''''none'),
    (
240 35'Bookmarks''/member/bookmarks''''none'),
    (
250 36'Logout''/logout.php''''none'),
    (
262531'Profile''/member/settings/profile''''none'),
    (
272532'Change Password''/member/settings/password''''none'),
    (
280 41'Menu 1''#''fa fw fa-home''none'),
    (
292841'Menu 1.1''#''''none'),
    (
302842'Menu 1.2''#''''none'),
    (
310 42'Menu 2''#''''none'),
    (
323141'Menu 2.1''#''''none'),
    (
333141'Menu 2.1.1''#''''none'),
    (
343142'Menu 2.1.2''#''''none'),
    (
353441'Menu 2.2''#''''none'),
    (
362131'Popular Files''/popular''''none'),
    (
372122'Top Authors''/top''''none'),
    (
382123'Wordpress''/wp''''none'); 
i want create dropdown filed
please see Attachment file
tnx
You could follow my tutorial up until "A HTML menu using <ul>s" (just use the ordered_list() to create the array). After that you pass that array to the form_dropdown() function (you must load the the form helper).

http://avenir.ro/revisiting-the-multilev...framework/