How To Create User Permissions View To Dynamic Sidebar Menu AdminLTE

Seegatesite.com – I’ll share how to create a user permissions view to dynamic sidebar menu on adminLTE using Mysql. Someone asks through this website, how do I create a user permission view for my application. Because if I explain through the comment form will be very long at all, so on this occasion I try to write tutorial my way to create user permissions view for dynamic sidebar menu adminLTE.

How To Create User Permissions View To Dynamic Sidebar Menu Adminlte

Before starting , you should read Creating dynamic sidebar menu with Mysql and AdminLTE Bootstrap Template

Create 3 pieces of tables t_user, t_menu, t_menu_sub to accommodate the data

t_user table

Create Table T User How To Create User Permission View To Dynamic Sidebar Menu Adminlte

CREATE TABLE `t_user` (
  `id_user` int(11) NOT NULL AUTO_INCREMENT,
  `name_user` varchar(20) NOT NULL,
  `pass_user` char(50) NOT NULL,
  `h_menu` varchar(254) DEFAULT NULL,
  PRIMARY KEY (`id_user`),
  UNIQUE KEY `NewIndex1` (`name_user`)
)

Fill the following data

insert  into `t_user`(`id_user`,`name_user`,`pass_user`,`h_menu`) values (1,'ADMIN','c44e503833b64e9f27197a484f4257c0','1,2,3,4,5,6,7,8,9');

r_menu table

Create Table R Menu How To Create User Permission View To Dynamic Sidebar Menu Adminlte

CREATE TABLE `r_menu` (
  `id_menu` int(2) NOT NULL AUTO_INCREMENT,
  `name_menu` varchar(100) DEFAULT NULL,
  `order` int(2) DEFAULT NULL,
  `icon` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id_menu`)
) 

Fill the following data

insert  into `r_menu`(`id_menu`,`name_menu`,`order`,`icon`) values (1,'Data Karyawan',1,'fa fa-dashboard');
insert  into `r_menu`(`id_menu`,`name_menu`,`order`,`icon`) values (2,'Penggajian',2,'fa fa-files-o');
insert  into `r_menu`(`id_menu`,`name_menu`,`order`,`icon`) values (3,'Penilaian Kinerja Karyawan',3,'fa fa-share');
insert  into `r_menu`(`id_menu`,`name_menu`,`order`,`icon`) values (4,'Transaksi',4,'fa fa-laptop');

r_menu_sub table

Create Table R Menu Sub How To Create User Permission View To Dynamic Sidebar Menu Adminlte

CREATE TABLE `r_menu_sub` (
`id_sub_menu` int(3) NOT NULL AUTO_INCREMENT,
`name_sub_menu` varchar(100) DEFAULT NULL,
`id_menu` int(2) DEFAULT NULL,
`order_sub_menu` int(2) DEFAULT NULL,
`url` varchar(150) DEFAULT '#',
`title` text,
PRIMARY KEY (`id_sub_menu`),
KEY `urutan_sub_menu_index` (`id_menu`,`order_sub_menu`)
)

Fill the following data

insert  into `r_menu_sub`(`id_sub_menu`,`name_sub_menu`,`id_menu`,`order_sub_menu`,`url`,`title`) values (1,'Data Karyawan',1,1,'aplikasi/karyawan/v_karyawan.php','Data karyawan - untuk mencatat biodata karyawan');
insert  into `r_menu_sub`(`id_sub_menu`,`name_sub_menu`,`id_menu`,`order_sub_menu`,`url`,`title`) values (2,'Otorisasi Resign',1,4,'aplikasi/karyawan/v_otorisasi_resign.php',NULL);
insert  into `r_menu_sub`(`id_sub_menu`,`name_sub_menu`,`id_menu`,`order_sub_menu`,`url`,`title`) values (3,'Data Parameter Gaji',2,1,'aplikasi/penggajian/v_parameter_gaji.php',NULL);
insert  into `r_menu_sub`(`id_sub_menu`,`name_sub_menu`,`id_menu`,`order_sub_menu`,`url`,`title`) values (4,'Proses Penggajian (Gaji, THR, TAT)',2,2,'#',NULL);
insert  into `r_menu_sub`(`id_sub_menu`,`name_sub_menu`,`id_menu`,`order_sub_menu`,`url`,`title`) values (5,'Pembayaran Komisi AO',2,3,'aplikasi/penggajian/v_komisi_ao.php',NULL);
insert  into `r_menu_sub`(`id_sub_menu`,`name_sub_menu`,`id_menu`,`order_sub_menu`,`url`,`title`) values (6,'Parameter Insentif Dinamis',3,1,'#',NULL);
insert  into `r_menu_sub`(`id_sub_menu`,`name_sub_menu`,`id_menu`,`order_sub_menu`,`url`,`title`) values (7,'Penilaian PA',3,2,'#',NULL);
insert  into `r_menu_sub`(`id_sub_menu`,`name_sub_menu`,`id_menu`,`order_sub_menu`,`url`,`title`) values (8,'Transaksi Lembur',4,1,'aplikasi/transaksi/v_transaksi_lembur.php',NULL);
insert  into `r_menu_sub`(`id_sub_menu`,`name_sub_menu`,`id_menu`,`order_sub_menu`,`url`,`title`) values (9,'Transaksi Lain (BUUT , KOPKAR , UUO-SPI)',4,2,'aplikasi/transaksi/v_transaksi_lain.php',NULL);

Here I will not explain in detail how to create the overall form on adminLTE but I will only discuss how to create a user permissions view on adminLTE dynamic sidebar menu , if you want the full code please download at the end of the article.

Script on the adminLTE sidebar menu

     <!-- sidebar: style can be found in sidebar.less -->
<section class="sidebar">
      <!-- Sidebar user panel -->
<ul class="sidebar-menu">
<li class="header">
<?php echo date('d/F/Y') ?>
        </li>
        <?php $query = mysql_query("SELECT * FROM r_menu ORDER BY r_menu.`order` ASC")?>
        <?php while($menu=mysql_fetch_array($query)) {?>
<li class="treeview">
          <a id="menu-<?php echo $menu['id_menu']; ?>"href="#">
              <i class="<?php echo $menu['icon']; ?>"></i>
              <span><?php echo $menu['name_menu']; ?></span> 
              <i class="fa fa-angle-left pull-right"></i>
          </a>
          <?php $id_menu=$menu['id_menu']; $query_sub_menu=mysql_query("select * from r_menu_sub where id_menu=$id_menu order by order_sub_menu asc"); if(mysql_num_rows($query_sub_menu)>0)
          { ?>
<ul class="treeview-menu">
           <?php $ilx=1; while($sub_menu=mysql_fetch_array($query_sub_menu)) { ?>
<li>
                            <?php $mymenu=$_SESSION['h_menu']; $arramymenu=explode(",", $mymenu) ; if(in_array($sub_menu['id_sub_menu'], $arramymenu)) { if($ilx % 2 == 0) { $circle_color = 'aqua'; }else { $circle_color = 'yellow'; } ?>
                            <a href="<?php echo $sitename.$sub_menu['url']; ?>" title="<?php echo $sub_menu['title']; ?>">
                                <i class="fa fa-circle-o text-<?php echo $circle_color; ?>"></i>
                                <span class="wrap">
                                    <?php echo $sub_menu['name_sub_menu']; ?>
                                </span>
                            </a>
                      </li>
          <?php $ilx++; } } ?>
          </ul>
        <?php } ?>
      </li>
      <?php } ;?>
    </ul>
  </section>
      <!-- /.sidebar -->

Explanation :

The main script to create dynamic menu with user permissions view is the following script

$mymenu=$_SESSION['h_menu'];
$arramymenu=explode(",", $mymenu) ;
if(in_array($sub_menu['id_sub_menu'], $arramymenu))
{  
//.......showing menu .........
}

$_SESSION[‘h_menu’] store h_menu value (user permission access field) from t_user table

in my example t_user table has the following value

Create User Permission Access

$_SESSION[‘h_menu’] has the value “1,2,3,4,5,6,7,8,9” > id_menu_sub. In my example, r_sub_menu has 9 rows of data with id_menu_sub 1,2,3,4,5,6,7,8,9

When table r_menu_sub looping, it will check if id_menu_sub match with user permission access value in array, if same it will show menu

if(in_array($sub_menu['id_sub_menu'], $arramymenu))
{
//.................
<a href="<?php echo $sitename.$sub_menu['url']; ?>" title="<?php echo $sub_menu['title']; ?>">
//.............................
}

Try to change the h_menu value to “1,3,4,5,7”. What will happen ?

Hopefully this brief tutorial can help you to create a user permissions view on adminLTE sidebar.

To get the full source code please download the following link

Thus article about How To Create User Permissions View To Dynamic Sidebar Menu AdminLTE 🙂

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

1 Comment

 Add your comment
  1. thanks
    very good

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: