Dynamic Dropdown Menu using Bootstrap 4 in CodeIgniter

If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter. Take note that I've used 3.1.7 in this tutorial. After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP. Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as ci_dynamic_dropdown so I'm using the below code.
  1. localhost/ci_dynamic_dropdown
codeigniter successful download

Removing index.php in our URLs

By default, index.php is included in every URL of our app. To remove this, please refer to How to Remove Index.Php in the URL of Codeigniter Application.

Creating our Database

I've included a .sql file located in db folder in the downloadable of this tutorial. All you need to do is import the file into your phpMyAdmin. If you have no idea how to import, please refer to my tutorial How import .sql file to restore MySQL database. You should be able to add a database named test.

Connecting our App into our Database

Next, we're going to connect our codeigniter application to the database that we created earlier. 1. In your codeigniter app folder, open database.php located in application/config folder. 2. Update database.php with your credential the same as what I did in the codes below.
  1. $db['default'] = array(
  2.         'dsn'   => '',
  3.         'hostname' => 'localhost',
  4.         'username' => 'root',
  5.         'password' => '',
  6.         'database' => 'test',
  7.         'dbdriver' => 'mysqli',
  8.         'dbprefix' => '',
  9.         'pconnect' => FALSE,
  10.         'db_debug' => (ENVIRONMENT !== 'production'),
  11.         'cache_on' => FALSE,
  12.         'cachedir' => '',
  13.         'char_set' => 'utf8',
  14.         'dbcollat' => 'utf8_general_ci',
  15.         'swap_pre' => '',
  16.         'encrypt' => FALSE,
  17.         'compress' => FALSE,
  18.         'stricton' => FALSE,
  19.         'failover' => array(),
  20.         'save_queries' => TRUE
  21. );

Creating our Model

Next, we create the model for our app. Take note that the first letter of your model name should be in CAPITAL letter and the name of the model should be the same as the file name to avoid confusion. Create a file named Category_model.php in application/models folder of our app and put the ff codes:
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Category_model extends CI_Model {
  5.         public function __construct(){
  6.                 parent::__construct();
  7.                 $this->load->database();
  8.         }
  9.         public function getAllCategories(){
  10.                 $query = $this->db->get('categories');
  11.                 return $query->result();
  12.         }
  13.  
  14. }

Creating our Controller

Next step is to create our controller. Controllers follow the same naming convention as models. Create a file named Main.php in application/controllers folder of our app and put the ff codes.
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Main extends CI_Controller {
  5.         public function __construct(){
  6.                 parent::__construct();
  7.                 $this->load->helper('url');
  8.                 $this->load->model('category_model');
  9.         }
  10.         public function index(){
  11.                 $data['categories'] = $this->category_model->getAllCategories();
  12.                 $this->load->view('home', $data);
  13.         }
  14.  
  15. }

Configuring our Default Controller

Next, we are going to set our default controller so that whenever we haven't set up a controller to use, this default controller will be used instead. Open routes.php located in application/config folder and set the default route to our user controller. Note: While we name controllers using CAPITAL letter in this first letter, we refer to them in SMALL letter.
  1. $route['default_controller'] = 'main';

Creating our View

Lastly, we create the views of our app. I've used Bootstrap 4 and jQuery in this tutorial which are included in the downloadables but if you want you may download them yourself. For Bootstrap 4 For jQuery Create the ff files inside application/views folder. home.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.         <meta charset="utf-8">
  5.         <title>Dynamic Dropdown Menu using Bootstrap 4 in CodeIgniter</title>
  6.         <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap4/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  10.         <a class="navbar-brand" href="#">SourceCodeSter</a>
  11.         <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
  12.         <span class="navbar-toggler-icon"></span>
  13.         </button>
  14.  
  15.         <div class="collapse navbar-collapse" id="navbarSupportedContent">
  16.         <ul class="navbar-nav mr-auto">
  17.                 <li class="nav-item dropdown">
  18.                         <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  19.                         Category
  20.                         </a>
  21.                         <div class="dropdown-menu" aria-labelledby="navbarDropdown">
  22.                                 <?php
  23.                                         foreach($categories as $category){
  24.                                                 echo "
  25.                                                         <a class='dropdown-item' href='#'>".$category->name."</a>
  26.                                                 ";
  27.                                         }
  28.                                 ?>
  29.                         </div>
  30.                 </li>
  31.         </ul>
  32.         </div>
  33. </nav>
  34.  
  35. <script src="<?php echo base_url(); ?>jquery.min.js"></script>
  36. <script src="<?php echo base_url(); ?>bootstrap4/js/bootstrap.min.js"></script>
  37. </body>
  38. </html>
That ends this tutorial. Happy Coding :)

Add new comment