Create Dynamic Sidebar Multilevel Menu in AdminLTE with MySQL ,PHP and PDO

Creating dynamic sidebar menu on adminLTE using MySQL, PHP, and PDO. Continuing my previous article Creating Dynamic sidebar menu with Mysql and AdminLTE Bootstrap Template. There are still many visitors who ask about the script. Here I will share a more complex tutorial, but it is easier to understand for beginners (hopefully). Let’s start the tutorial

Create Dynamic Sidebar Multilevel Menu In AdminLTE With MySQL ,PHP And PDO

Tutorial how to create dynamic sidebar multilevel menu in MySQL, PHP, and AdminLTE template

1. Create new MySQL database with name “dbmenu
2. Please create 3 new table with the name r_menu, r_menu_sub, r_menu_sub_level_2
3. Insert three tables with the following query

r_menu table

R Menu Table Create Dynamic Sidebar Multilevel Menu In AdminLTE With MySQL ,PHP And PDO


insert  into <code>r_menu</code>(<code>id_menu</code>,<code>nama_menu</code>,<code>urutan</code>,<code>icon</code>,<code>newclass</code>) values (1,'Multilevel Menu',1,'&lt;i class=\&quot;fa fa-dashboard\&quot;&gt;&lt;/i','first_class');
insert  into <code>r_menu</code>(<code>id_menu</code>,<code>nama_menu</code>,<code>urutan</code>,<code>icon</code>,<code>newclass</code>) values (2,'Penjualan',2,'&lt;i class=\&quot;fa fa-files-o\&quot;&gt;&lt;/i','second_class');
insert  into <code>r_menu</code>(<code>id_menu</code>,<code>nama_menu</code>,<code>urutan</code>,<code>icon</code>,<code>newclass</code>) values (3,'Pembelian',3,'&lt;i class=\&quot;fa fa-th\&quot;&gt;&lt;/i','third_class');
insert  into <code>r_menu</code>(<code>id_menu</code>,<code>nama_menu</code>,<code>urutan</code>,<code>icon</code>,<code>newclass</code>) values (4,'Laporan',6,'&lt;i class=\&quot;fa fa-dashboard\&quot;&gt;&lt;/i',NULL);
insert  into <code>r_menu</code>(<code>id_menu</code>,<code>nama_menu</code>,<code>urutan</code>,<code>icon</code>,<code>newclass</code>) values (5,'Utility',7,'&lt;i class=\&quot;fa fa-pie-chart\&quot;&gt;&lt;/i',NULL);
insert  into <code>r_menu</code>(<code>id_menu</code>,<code>nama_menu</code>,<code>urutan</code>,<code>icon</code>,<code>newclass</code>) values (6,'Mutasi Barang',4,'&lt;i class=\&quot;fa fa-laptop\&quot;&gt;&lt;/i',NULL);
insert  into <code>r_menu</code>(<code>id_menu</code>,<code>nama_menu</code>,<code>urutan</code>,<code>icon</code>,<code>newclass</code>) values (7,'Biaya',5,'&lt;i class=\&quot;fa fa-envelope\&quot;&gt;&lt;/i',NULL);

r_menu_sub table

R Menu Sub Table Create Dynamic Sidebar Multilevel Menu In AdminLTE With MySQL ,PHP And PDO

insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (1,'Jenis Barang',1,3,'2','','&lt;li class=\&quot;dropdown-header\&quot;&gt;Referensi Produk&lt;/li&gt;',NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Master Jenis Barang','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (2,'Merk',1,4,'2','',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Master Merk Barang','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (5,'Pelanggan',2,1,'2','aplikasi/penjualan/v_pelanggan.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Master Pelanggan','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (6,'Penjualan',2,2,'2','aplikasi/penjualan/v_penjualan.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Transaksi Penjualan (POS)','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (7,'Retur Penjualan',2,3,'2','aplikasi/penjualan/v_retur_penjualan.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Transaksi Retur Penjualan','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (8,'Supplier',3,1,'4','aplikasi/pembelian/v_master_supplier.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Master Supplier','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (9,'Pembelian',3,2,'4','aplikasi/pembelian/v_pembelian.php',NULL,'','&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Transaksi Pembelian','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (10,'Retur Pembelian',3,3,'4','aplikasi/pembelian/v_retur_pembelian.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Transaksi Retur Pembelian','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (11,'Cetak Daftar Harga Barang',4,4,'2','aplikasi/laporan/l_daftar_harga_barang.php','',NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Daftar Harga Barang','_blank');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (12,'Cetak Daftar Harga Jasa',4,5,'2','aplikasi/laporan/l_daftar_harga_jasa.php','',NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Daftar Harga Jasa','_blank');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (13,'L. Pembelian',4,8,'4','aplikasi/laporan/v_laporan_pembelian.php','',NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Pembelian','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (14,'L. Laba Rugi',4,14,'4','aplikasi/laporan/v_laporan_laba_rugi.php','&lt;li class=\&quot;divider\&quot;&gt;&lt;/li&gt;',NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Laba Rugi','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (15,'Master Pengguna Aplikasi',5,1,'4','aplikasi/utility/v_mstuser.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Master Pengguna Aplikasi','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (16,'Referensi Sistem',5,2,'4','aplikasi/utility/v_refsystem.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Referensi Sistem','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (17,'Satuan',1,5,'2','aplikasi/master/v_satuan.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Master Satuan','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (18,'Pelunasan Jual Tempo',2,4,'2','aplikasi/penjualan/v_lunas_jual_tempo.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Form Pelunasan Penjualan Secara Tempo','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (20,'Pelunasan Beli Tempo',3,4,'4','aplikasi/pembelian/v_lunas_beli_tempo.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Form Pelunasan Pembelian Tempo','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (21,'Penyesuaian Stok',6,1,'2','aplikasi/mutasi/v_mutasi.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Menu Penyesuaian Stok (Adjustment Stok)','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (22,'Kartu Stok',6,2,'2','aplikasi/mutasi/v_kartu_stok.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Lihat Kartu Stok','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (23,'Cetak Mutasi Stok Barang',6,3,'2','aplikasi/mutasi/l_laporan_mutasi_stok.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Mutasi Stok Barang','_blank');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (24,'Cetak Barang Habis',6,4,'2','aplikasi/mutasi/l_barang_abis.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Barang Habis','_blank');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (25,'Cetak Daftar Harga Jual dan Beli',4,3,'4','aplikasi/laporan/l_daftar_seluruh_harga.php',NULL,'&lt;li class=\&quot;divider\&quot;&gt;&lt;/li&gt;','&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Daftar Harga Jual dan Beli','_blank');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (26,'L. Penjualan',4,7,'2','aplikasi/laporan/v_laporan_penjualan.php','&lt;li class=\&quot;divider\&quot;&gt;&lt;/li&gt;',NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Penjualan','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (27,'Cetak Daftar Supplier',4,1,'4','aplikasi/laporan/l_daftar_supplier.php',NULL,'&lt;li class=\&quot;divider\&quot;&gt;&lt;/li&gt;','&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Daftar Supplier','_blank');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (28,'Cetak Daftar Pelanggan',4,2,'2','aplikasi/laporan/l_daftar_pelanggan.php',NULL,'&lt;li class=\&quot;divider\&quot;&gt;&lt;/li&gt;','&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Daftar Pelanggan','_blank');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (30,'L. Retur Penjualan',4,9,'2','aplikasi/laporan/v_laporan_retur_penjualan.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Retur Penjualan','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (31,'L. Retur Pembelian',4,10,'4','aplikasi/laporan/v_laporan_retur_pembelian.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Retur Pembelian','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (32,'Penjualan Grosir',2,5,'2','aplikasi/penjualan/v_penjualan_grosir.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Transaksi Penjualan Grosir','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (33,'Backup Database',5,3,'4','aplikasi/utility/v_backup.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Menu Backup Database','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (34,'Closing Stok',6,5,'4','aplikasi/mutasi/v_close_stok.php','&lt;li class=\&quot;divider\&quot;&gt;&lt;/li&gt;',NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Menu Penutupan Stok (Closing Stok)','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (35,'Ubah Margin Manual',5,4,'4','aplikasi/utility/v_ubah_margin.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Menu Ubah Laba Rugi secara Manual Pada Transaksi Penjualan','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (36,'Biaya Harian',7,1,'2','aplikasi/biaya/v_biaya.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Untuk mencatat pengeluaran harian','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (37,'Setoran',7,2,'2','aplikasi/biaya/v_setoran.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Untuk mencatat setoran ','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (38,'Upah Karyawan',7,3,'4','aplikasi/biaya/v_upah.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Untuk mencatat pengeluaran gaji karyawan','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (39,'L. Biaya/Pengeluaran Harian',4,11,'2','aplikasi/laporan/v_laporan_biaya.php','&lt;li class=\&quot;divider\&quot;&gt;&lt;/li&gt;',NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Biaya / pengeluaran harian','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (41,'L. Setoran',4,12,'2','aplikasi/laporan/v_laporan_setoran.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan Setoran','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (42,'L. Upah Karyawan',4,13,'4','aplikasi/laporan/v_laporan_upah.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan upah karyawan','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (43,'L. Transaksi Harian',4,6,'2','aplikasi/laporan/v_transaksi_harian.php','&lt;li class=\&quot;divider\&quot;&gt;&lt;/li&gt;','','&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Laporan transaksi harian','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (44,'Restore Master Barang',5,5,'4','aplikasi/utility/v_restore_m_barang.php','&lt;li class=\&quot;divider\&quot;&gt;&lt;/li&gt;',NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Restoring Master Barang yang dihapus','_self');
insert  into <code>r_menu_sub</code>(<code>id_sub_menu</code>,<code>nama_sub_menu</code>,<code>id_menu</code>,<code>urutan_sub_menu</code>,<code>hak_akses</code>,<code>url</code>,<code>content_before</code>,<code>content_after</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (45,'Restore Master Jasa',5,6,'4','aplikasi/utility/v_restore_m_jasa.php',NULL,NULL,'&lt;i class=\&quot;fa fa-arrow-right\&quot;&gt;&lt;/i&gt;','Restoring Master Jasa yang dihapus','_self');

r_menu_sub_level_2 table

R Menu Sub Level 2 Table Create Dynamic Sidebar Multilevel Menu In AdminLTE With MySQL ,PHP And PDO

insert  into <code>r_menu_sub_level_2</code>(<code>id_sub_menu_level_2</code>,<code>nama_sub_menu_level_2</code>,<code>id_sub_menu</code>,<code>urutan_sub_menu</code>,<code>url</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (1,'multilevel 1',1,1,'aplikasi/master/v_jenis_produk.php','&lt;i class=\&quot;fa fa-circle\&quot;&gt;&lt;/i&gt;','this is multilevel','_self');
insert  into <code>r_menu_sub_level_2</code>(<code>id_sub_menu_level_2</code>,<code>nama_sub_menu_level_2</code>,<code>id_sub_menu</code>,<code>urutan_sub_menu</code>,<code>url</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (2,'multilevel 2',1,2,'aplikasi/master/v_merk.php','&lt;i class=\&quot;fa fa-circle\&quot;&gt;&lt;/i&gt;','this is multilevel','_self');
insert  into <code>r_menu_sub_level_2</code>(<code>id_sub_menu_level_2</code>,<code>nama_sub_menu_level_2</code>,<code>id_sub_menu</code>,<code>urutan_sub_menu</code>,<code>url</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (3,'multilevel 3',1,3,'aplikasi/master/v_jasa.php','&lt;i class=\&quot;fa fa-circle\&quot;&gt;&lt;/i&gt;','this is multilevel','_self');
insert  into <code>r_menu_sub_level_2</code>(<code>id_sub_menu_level_2</code>,<code>nama_sub_menu_level_2</code>,<code>id_sub_menu</code>,<code>urutan_sub_menu</code>,<code>url</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (4,'multilevel 4',1,4,'aplikasi/master/v_barang.php','&lt;i class=\&quot;fa fa-circle\&quot;&gt;&lt;/i&gt;','this is multilevel','_self');
insert  into <code>r_menu_sub_level_2</code>(<code>id_sub_menu_level_2</code>,<code>nama_sub_menu_level_2</code>,<code>id_sub_menu</code>,<code>urutan_sub_menu</code>,<code>url</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (5,'multilevel 5',2,2,'aplikasi/penjualan/v_pelanggan.php','&lt;i class=\&quot;fa fa-circle\&quot;&gt;&lt;/i&gt;','this is multilevel','_self');
insert  into <code>r_menu_sub_level_2</code>(<code>id_sub_menu_level_2</code>,<code>nama_sub_menu_level_2</code>,<code>id_sub_menu</code>,<code>urutan_sub_menu</code>,<code>url</code>,<code>icon</code>,<code>title</code>,<code>target</code>) values (6,'multilevel 6',2,1,'aplikasi/penjualan/v_penjualan.php','&lt;i class=\&quot;fa fa-circle\&quot;&gt;&lt;/i&gt;','this is multilevel','_self');

4. Create new project folder with the named “dinamikmenu“.
5. Copying AdminLTE template in the dinamikmenu folder. Please download an adminLTE template here if you doesn’t have it.

Folder Structure

6. Create a new folder with name “model” in the dinamikmenu folder. This folder is used to store 2 PHP file that is used to connect PHP and MySQL with PDO.
7. Create a PHP file with the name sql.php and dbconn.php. Copy the following code

dbconn.php

<?php class dbconn { public $dblocal; public function __construct() { } public function initDBO() { $this->dblocal = new PDO("mysql:host=localhost;dbname=dbmenu;charset=latin1","root","123456",array(PDO::ATTR_PERSISTENT => true));
		$this->dblocal->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	}	
}
?>

Description :

A class above are used to connect MySQL database with PHP using PDO. Customize with your database settings

PDO("mysql:host=localhost;dbname=dbmenu;charset=latin1","your-database-user","your-database-password",array(PDO::ATTR_PERSISTENT => true))

sql.php

<?php class sql extends dbconn { public function __construct() { $this->initDBO();
    	}
      public function getMenu()
      {
           $db = $this->dblocal;
           try
           {
            $stmt = $db->prepare("select * from r_menu order by urutan");
            $stmt->execute();
            $stat[0] = true;
            $stat[1] = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return $stat;
          }
          catch(PDOException $ex)
          {
            $stat[0] = false;
            $stat[1] = $ex->getMessage();
            return $stat;
          }
      }
      public function getSubMenu($id)
      {
          $db = $this->dblocal;
          try
          {
            $stmt = $db->prepare("select * from r_menu_sub where id_menu = :id order by urutan_sub_menu asc");
            $stmt->bindParam("id",$id);
            $stmt->execute();
            $stat[0] = true;
            $stat[1] = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return $stat;
          }
          catch(PDOException $ex)
          {
            $stat[0] = false;
            $stat[1] = $ex->getMessage();
            return $stat;
          }
      }
      public function getSubMenuLevel2($id)
      {
          $db = $this->dblocal;
          try
          {
            $stmt = $db->prepare("select * from r_menu_sub_level_2 where id_sub_menu = :id order by urutan_sub_menu asc");
            $stmt->bindParam("id",$id);
            $stmt->execute();
            $stat[0] = true;
            $stat[1] = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return $stat;
          }
          catch(PDOException $ex)
          {
            $stat[0] = false;
            $stat[1] = $ex->getMessage();
            return $stat;
          }
      }
}

?>

Description :

A class above are used to accommodate the function query the database using PDO.

public function getMenu() : used to query table r_menu

getSubMenu($id) : used to query table r_menu_sub based id_menu

getSubMenuLevel2($id) : used to query table r_menu_sub_level_2 based id_sub_menu

8. Copy the code below and paste the following code below adminLTE sidebar.

 <!-- sidebar menu: : style can be found in sidebar.less -->
      

<ul class="sidebar-menu">
        

<li class="header">MAIN NAVIGATION</li>


        <?php require_once("model/dbconn.php"); require_once("model/sql.php"); $query = new sql(); $data = $query->getMenu();
          $key = $data[1]; 
          foreach($key as $menu) 
          {
        ?>
          	

<li class="treeview">
	          	<a href="#">
	            	<?php echo $menu['icon']?> <span><?php echo $menu['nama_menu'];?></span>
	            	<span class="pull-right-container">
	             	 	<i class="fa fa-angle-left pull-right"></i>
	            	</span>
	          	</a>
	          	<?php $sub_menu = $query->getSubMenu($menu['id_menu']);
              		$count_submenu = count($sub_menu[1]);
              		if($count_submenu >=1)
              		{
              	?>


<ul class="treeview-menu <?php echo $menu['newclass'];?>">
              			<?php foreach ($sub_menu[1] as $key ) { ?>
              					

<li>
              						<?php $sub_menu_level_2 = $query->getSubMenuLevel2($key['id_sub_menu']);
						              	$count_submenu_level_2 = count($sub_menu_level_2[1]);
              						?>
              						<a href="<?php echo $key['url']; ?>" target="<?php echo $var['target']; ?>" title="<?php echo $key['title']; ?>">
				                        <?php echo $key['icon'].$key['nama_sub_menu']; ?>
				                        <?php if($count_submenu_level_2 >=1)
						              		{
						           		?>
						                        <span class="pull-right-container">
								             	 	<i class="fa fa-angle-left pull-right"></i>
								            	</span>
								        <?php } ?>
				                    </a>
				                    <?php if($count_submenu_level_2 >=1)
						              		{
						            ?>


<ul class="treeview-menu">
						              			<?php foreach ($sub_menu_level_2[1] as $var ) { ?>
						              					

<li>
						              						<a href="<?php echo $var['url']; ?>" title="<?php echo $var['title']; ?>" target="<?php echo $var['target']; ?>" >
										                        <?php echo $var['icon'].$var['nama_sub_menu_level_2']; ?>
										                    </a>
						              					</li>


						              			<?php } ?>
							          			</ul>


						              	<?php } ?>
              					</li>


              			<?php } ?>
	          			</ul>


              	<?php } ?>
	          	
        	</li>


        <?php } ?>
        
      </ul>


Or download the entire full source code in the bottom article

Up to this point, you have finished creating dynamic sidebar menu adminLTE. Try on your browser and make sure as shown below :

Tutorial How To Create Dynamic Sidebar Menu On AdminLTE With Php And Mysql

How ? very easy right

To download the full source code above, please download via the following URL. To open a URL link, please share this article by pressing the share button below.

Thus the article about Create Dynamic Sidebar Multilevel Menu in AdminLTE with MySQL ,PHP, and PDO, hope useful:)

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.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: