• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Build a menu + submenu from database

#1
[eluser]redraw[/eluser]
Dear all,

sorry for my bad english.
I want build a menu + submenu from database :
here my database :

Code:
/*
SQLyog Ultimate - MySQL GUI v8.2
MySQL - 5.1.33-community-log : Database - dbrsmp
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`dbrsmp` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `dbrsmp`;

/*Table structure for table `ms_akses` */

DROP TABLE IF EXISTS `ms_akses`;

CREATE TABLE `ms_akses` (
  `akses_id` int(10) DEFAULT NULL,
  `tgl_on` date DEFAULT NULL,
  `tgl_off` date DEFAULT NULL,
  `aktif` tinyint(4) DEFAULT NULL,
  `program_id` int(10) DEFAULT NULL
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

/*Data for the table `ms_akses` */

insert  into `ms_akses`(`akses_id`,`tgl_on`,`tgl_off`,`aktif`,`program_id`) values (1,'0000-00-00',NULL,NULL,1),(2,'0000-00-00',NULL,NULL,1),(1,'0000-00-00',NULL,NULL,3),(1,'0000-00-00',NULL,NULL,2);

/*Table structure for table `ms_program` */

DROP TABLE IF EXISTS `ms_program`;

CREATE TABLE `ms_program` (
  `program_id` int(10) NOT NULL AUTO_INCREMENT,
  `program` varchar(50) DEFAULT NULL,
  `subprogram_id` int(10) DEFAULT NULL,
  `link` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`program_id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

/*Data for the table `ms_program` */

insert  into `ms_program`(`program_id`,`program`,`subprogram_id`,`link`) values (1,'AKUNTANSI',1,'1'),(2,'FARMASI',2,'2'),(3,'LABORATORIUM',NULL,'3'),(4,'RADIOLOGI',NULL,'4'),(5,'PENDAFTARAN POLI',NULL,'5'),(6,'PENDAFTARAN UGD',NULL,'6');

/*Table structure for table `ms_subprogram` */

DROP TABLE IF EXISTS `ms_subprogram`;

CREATE TABLE `ms_subprogram` (
  `subprogram_id` int(10) DEFAULT NULL,
  `subprogram` varchar(50) DEFAULT NULL
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

/*Data for the table `ms_subprogram` */

insert  into `ms_subprogram`(`subprogram_id`,`subprogram`) values (1,'GL'),(2,'Obat'),(2,'Abit');

/*!40101 SET [email protected]_SQL_MODE */;
/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */;
/*!40014 SET [email protected]_UNIQUE_CHECKS */;
/*!40111 SET [email protected]_SQL_NOTES */;

my model :
Code:
class Menumodel extends Model{
    function Menumodel()
    {
        parent::Model();
    }

      function ambil_menu()
    {
        
        $this->db->select('*');
        $this->db->from('ms_user');
        $this->db->join('ms_akses', 'ms_user.akses_id = ms_akses.akses_id');
        $this->db->join('ms_program', 'ms_akses.program_id = ms_program.program_id');
        $this->db->join('ms_subprogram', 'ms_program.subprogram_id = ms_subprogram.subprogram_id');
        $this->db->where('ms_user.user_id',from_session('user_id'));
        
        $data = $this->db->get();        
        return $data;
        
        $data = $this->db->get();        
        return $data;
      }
}

my controller :
Code:
function index()
    {

    $data['detail'] = $this->menumodel->ambil_menu();
    $this->load->view('haluser/index',$data);
    }

my view :
Code:
<ul class="dropdown dropdown-horizontal">
&lt;?php foreach ($detail->result() as $rows) : ?&gt;
    <li>&lt;?php echo anchor('home/'.$rows->program,$rows->program); ?&gt;
        <ul>
            <li>&lt;?php echo anchor('home/'.$rows->subprogram,$rows->subprogram); ?&gt;</li>
        </ul>
    </li>
    &lt;?php endforeach; ?&gt;
</ul>

The menu and sub menu can show up on the site, but it's goes wrong

user which have user_id = 'admin' it should have 3 menu
menu 1 with 1 sub menu (gl)
menu 2 with 2 sub menu (obat, abit)
menu 3 without sub menu

but in the view.php it's only show menu 1 and 2 which mean it's only show the menu that have submenu. the menu which dont have a submenu it's not show up.

not only that..
menu 2 is double show up on the view.php with each sub menu.. like this

menu 2 -> sub menu 1 (obat)
menu 2 -> sub menu 2 (abit)

not just like this :
menu 2 ->2 sub menu 1 (obat) and sub menu 2 (abit)

all i want is all menu show up somehow depending user access on database, not depending sub menu

please help me..
thank you

#2
[eluser]bgreene[/eluser]
you need a "left join" to get records which have no sub records
ie $this->db->join('comments', 'comments.id = blogs.id', 'left');

#3
[eluser]redraw[/eluser]
@bgreene
yes you right.. thank you so much

but i still got a problem
menu which have 2 or more sub-menu, show twice . example :
menu 2 have 2 sub-menu

in the site show like this :
menu 2 -> sub-menu 1
menu 2 -> sub-menu 2

not just only like this :
menu 2 -> sub-menu 1 & sub-menu 2

please advice..

Thanks

#4
[eluser]bgreene[/eluser]
you need a better loop, something like the following
$output = '';
$crntmenu = '';
$subcount = 0;
foreach rows as $row {
if ($row->mainmenu == $crntmenu){
// append submenu item
if ($subcount == 0) $output = <ul>
$output .= <li> $row->submenu </li>
$subcount++;
} else {
// new mainmenu item
if $subcount > 0 $output .= </ul>
$subcount = 0;
if ($crntmenu == '') $output .= <ul><li><li> $row->mainmenu
else $output .= </ul></li><li> $row->mainmenu
$crntmenu = $row->mainmenu
}
}
if ($crntmenu != '') $output .= </ul>
echo $output

#5
[eluser]redraw[/eluser]
thanks for the fast answer man.. i really appreciate it.
I transform your logical code above to my script :
Code:
&lt;?php
$output = '';
$crntmenu = '';
$subcount = 0;
foreach ($detail->result() as $row) {
if ($row->subprogram == $crntmenu){
  // append submenu item
      if ($row->subprogram == 0) {
          $output = '<ul class="dropdown dropdown-horizontal">';
          $output = '<li>';
          echo anchor('haluser/'.$row->subprogram,$row->subprogram);
          echo '</li>';
          $subcount++;
      } else {
          // new program item
          if ($subcount > 0) {
          $output= '</ul>';
          $subcount = 0;
              if ($crntmenu == '') {
                  $output = '<ul><li><li>';
                  echo anchor('haluser/'.$row->program,$row->program);
              }else{
                  $output = '</ul></li><li>';
                  echo anchor('haluser/'.$row->program,$row->program);
                  $crntmenu = $row->program;
            }

    }
}

                if ($crntmenu != '') {
                    $output = '</ul>';
                    echo $output;
                    }
}
}
?&gt;

when i call it.. i have just one single link = http:\\localhost\mysite\index.php\home\
not menus
how to fix it? i am so sory.. i am very beginner for this

Thank you

#6
[eluser]bgreene[/eluser]
$output .= (the . before the = turns it into append)

#7
[eluser]redraw[/eluser]
i turn . to append :

Code:
if ($crntmenu != '') {
        $output append = '</ul>';
        echo $output;
}

when i do that. it give me parse error...
please advice

#8
[eluser]bgreene[/eluser]
no,no,no you should type it as it is ie $output .= '</ul>'
".=" tells php to append the '</ul>' to whatever is already there

#9
[eluser]redraw[/eluser]
sorry for this misunderstanding

i turn the code like you said (all the $output now like this :
Code:
$output .= '</ul>';

but it make no different..
the site just show one single link = http:\\localhost\mysite\index.php\home\

thanks

#10
[eluser]bgreene[/eluser]
$output .= '<li>'. anchor('haluser/'.$row->subprogram,$row->subprogram).'</li>';

only use echo at the very end


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.