Haloo! Selamat Datang di Komunitas Private Training Web Development (56323 Members, 634 Products, 4510 Topic, 11896 Comments)

Butuh karyawan baru? atau seorang Web Developer (Part-time/Freelance/Full-time), cari disini : Members Area Jobs!

Membuat kategori dropdown seperti menu di Framework Codeigniter

Dibuat
Login Terakhir 2 Hari lalu,
Telah Dilihat 675 Kali
Ahmad Adzan Azari Mengatakan : Premium
  1. Assalamualaikum Warahmatullahi Wabarakatuh... saya ingin menampilkan kategori dan kategorinya sebagian ada yang dropdown seperti menu itu gimana ya penerapannya.mohon bantuannya mas?
  2.  
  3. //html
  4.  
  5. <ul class="pric_feat">
  6. <li class="mb-2"><a href="#">All</a></li>
  7. <li class="mb-2 dropdown-btn">Accessories
  8. <i class="mdi mdi-chevron-down"></i>
  9. </li>
  10. <div class="dropdown-container">
  11. <a href="#"><i class="mdi mdi-checkbox-marked-circle"></i> Basket</a>
  12. <a href="#"><i class="mdi mdi-checkbox-marked-circle"></i> Bowl</a>
  13. <a href="#"><i class="mdi mdi-checkbox-marked-circle"></i> Candle Holder</a>
  14. </div>
  15. <li class="mb-2"><a href="#">Mirror</a></li>
  16. <li class="mb-2"><a href="#">Stool</a></li>
  17. <li class="mb-2"><a href="#">Table</a></li>
  18. <li class="mb-2"><a href="#">Wall Decor</a></li>
  19. <li class="mb-2"><a href="#">Other</a></li>
  20. </ul>
Ada 2 File Kiriman :
1. db.PNG (39 KB)
2. hasil-diharapkan.PNG (8 KB)

Ada 6 Jawaban

Bukhori Muslim
commented on 29 Jul 2019 06:48:47
  1. Assalamualaikum,.. ini carfanya sama dengan menampilakn menu dropdown pada swarakalibata,
  2. coba buka database swarakalibata dan ke table menu, strukturnya hampir sama,
  3. tinggal modifikasi saja pada tag htmlnya menjadi seperti di atas :
  4.  
  5. function main_menu() {
  6. $ci = & get_instance();
  7. $query = $ci->db->query("SELECT id_menu, nama_menu, link, id_parent FROM menu where aktif='Ya' AND position='Bottom' order by urutan");
  8. $menu = array('items' => array(),'parents' => array());
  9. foreach ($query->result() as $menus) {
  10. $menu['items'][$menus->id_menu] = $menus;
  11. $menu['parents'][$menus->id_parent][] = $menus->id_menu;
  12. }
  13. if ($menu) {
  14. $result = build_main_menu(0, $menu);
  15. return $result;
  16. }else{
  17. return FALSE;
  18. }
  19. }
  20.  
  21. function build_main_menu($parent, $menu) {
  22. $html = "";
  23. if (isset($menu['parents'][$parent])) {
  24. if ($parent=='0'){
  25. $html .= "<ul class='the-menu'>
  26. <li><a href='".base_url()."' style='background: url(".base_url()."asset/images/home.png) no-repeat center; font-size:0; width:34px;'><br></a></li>";
  27. }else{
  28. $html .= "<ul>";
  29. }
  30. foreach ($menu['parents'][$parent] as $itemId) {
  31. if (!isset($menu['parents'][$itemId])) {
  32. if(preg_match("/^http/", $menu['items'][$itemId]->link)) {
  33. $html .= "<li><a target='_BLANK' href='".$menu['items'][$itemId]->link."'>".$menu['items'][$itemId]->nama_menu."</a></li>";
  34. }else{
  35. $html .= "<li><a href='".base_url().''.$menu['items'][$itemId]->link."'>".$menu['items'][$itemId]->nama_menu."</a></li>";
  36. }
  37. }
  38. if (isset($menu['parents'][$itemId])) {
  39. if(preg_match("/^http/", $menu['items'][$itemId]->link)) {
  40. $html .= "<li><a target='_BLANK' href='".$menu['items'][$itemId]->link."'><span>".$menu['items'][$itemId]->nama_menu."</span></a>";
  41. }else{
  42. $html .= "<li><a href='".base_url().''.$menu['items'][$itemId]->link."'><span>".$menu['items'][$itemId]->nama_menu."</span></a>";
  43. }
  44. $html .= build_main_menu($itemId, $menu);
  45. $html .= "</li>";
  46. }
  47. }
  48. $html .= "</ul>";
  49. }
  50. return $html;
  51. }
  52. echo main_menu();
Ahmad Adzan Azari
commented on 10 Agu 2019 17:45:00
  1. Assalamualaikum,.. begini mas. tapi tampilannya malah blank putih tampilan websitenya
  2.  
  3. <?php
  4. function main_menu() {
  5. $ci = & get_instance();
  6. $query = $ci->db->query("SELECT id_kategori, nama_kategori, kategori_seo, id_parent FROM tb_kategori where aktif='Y' order by id_kategori");
  7. $menu = array('items' => array(),'parents' => array());
  8. foreach ($query->result() as $menus) {
  9. $menu['items'][$menus->id_menu] = $menus;
  10. $menu['parents'][$menus->id_parent][] = $menus->id_menu;
  11. }
  12. if ($menu) {
  13. $result = build_main_menu(0, $menu);
  14. return $result;
  15. }else{
  16. return FALSE;
  17. }
  18. }
  19.  
  20. function build_main_menu($parent, $menu) {
  21. $html = "";
  22. if (isset($menu['parents'][$parent])) {
  23. if ($parent=='0'){
  24. $html .= "<ul>
  25. <li><a href='".base_url()."'>All</a></li>";
  26. }else{
  27. $html .= "<ul class='submenu'>";
  28. }
  29. foreach ($menu['parents'][$parent] as $itemId) {
  30. if (!isset($menu['parents'][$itemId])) {
  31. if(preg_match("/^http/", $menu['items'][$itemId]->link)) {
  32. $html .= "<li><a href='".$menu['items'][$itemId]->link."'>".$menu['items'][$itemId]->nama_menu."</a></li>";
  33. }else{
  34. $html .= "<li><a href='".base_url().''.$menu['items'][$itemId]->link."'>".$menu['items'][$itemId]->nama_menu."</a></li>";
  35. }
  36. }
  37. if (isset($menu['parents'][$itemId])) {
  38. if(preg_match("/^http/", $menu['items'][$itemId]->link)) {
  39. $html .= "<li><a href='".$menu['items'][$itemId]->link."'><span>".$menu['items'][$itemId]->nama_menu."</span></a>";
  40. }else{
  41. $html .= "<li><a href='".base_url().''.$menu['items'][$itemId]->link."'><span>".$menu['items'][$itemId]->nama_menu."</span></a>";
  42. }
  43. $html .= build_main_menu($itemId, $menu);
  44. $html .= "</li>";
  45. }
  46. }
  47. $html .= "</ul>";
  48. }
  49. return $html;
  50. }
  51. echo main_menu();
  52. ?>
  53.  
  54.  
Ahmad Adzan Azari
commented on 14 Agu 2019 17:05:42
  1. Assalamualaikum,.. Up
Bukhori Muslim
commented on 15 Agu 2019 07:17:37
  1. Assalamualaikum,.. ya pastinya blank lah gan, lebih teliti lagi jika ngoding,
  2. pastikan di sesuaikan di coding, ini masih ada 'id_menu' sedangkan di database/query antum gak ada :
  3. $menu['items'][$menus->id_menu] = $menus;
  4.  
  5. ini masih 'nama_menu' sedangkan di database/query antum gak ada :
  6. ".$menu['items'][$itemId]->nama_menu."
Bukhori Muslim
commented on 15 Agu 2019 07:18:59
  1. Assalamualaikum,.. struktur htmlnya juga belum disesuaikan dengan struktur yang antum minta di atas.
  2. walaupun querynya jalan namun tidak akan muncul juga seperti yang di inginkan.
Bukhori Muslim
commented on 15 Agu 2019 07:19:30
  1. Assalamualaikum,.. Coba ini :
  2. function main_menu() {
  3. $ci = & get_instance();
  4. $query = $ci->db->query("SELECT id_kategori as id_menu, nama_kategori as nama_menu, kategori_seo as link, id_parent FROM tb_kategori where aktif='Y'");
  5. $menu = array('items' => array(),'parents' => array());
  6. foreach ($query->result() as $menus) {
  7. $menu['items'][$menus->id_menu] = $menus;
  8. $menu['parents'][$menus->id_parent][] = $menus->id_menu;
  9. }
  10. if ($menu) {
  11. $result = build_main_menu(0, $menu);
  12. return $result;
  13. }else{
  14. return FALSE;
  15. }
  16. }
  17.  
  18. function build_main_menu($parent, $menu) {
  19. $html = "";
  20. if (isset($menu['parents'][$parent])) {
  21. if ($parent=='0'){
  22. $html .= "<ul class='pric_feat'>";
  23. }else{
  24. $html .= "<div class='dropdown-container'>";
  25. }
  26. foreach ($menu['parents'][$parent] as $itemId) {
  27. if (!isset($menu['parents'][$itemId])) {
  28. $html .= "<li class='mb-2'><a href='#'>".$menu['items'][$itemId]->nama_menu."</a></li>";
  29. }
  30. if (isset($menu['parents'][$itemId])) {
  31. $html .= "<a href='#'>".$menu['items'][$itemId]->nama_menu."</a>";
  32. $html .= build_main_menu($itemId, $menu);
  33. $html .= "</li>";
  34. }
  35. }
  36. if ($parent=='0'){
  37. $html .= "</ul>";
  38. }else{
  39. $html .= "</div>";
  40. }
  41. }
  42. return $html;
  43. }
  44. echo main_menu();
Maaf, Untuk Memberikan Komentar Anda Harus Login !!!