Loading Drop Down Selection Using PHP

If you are looking for on how to create Loading Drop Down Selection Using PHP then you are at the right place. This program works when the user selects from the drop-down list then the data from MySQL Table will display in the corresponding id. So, let's start with:

Creating our Table

We are going to make our database. To create a database:
  1. Open the PHPMyAdmin.
  2. Create a database and name it as "dropDown_test".
  3. After creating a database name, then we are going to create our table. And name it as "categories" and "products".
  4. Kindly copy the code below.
For the Categories Table
  1. --
  2. -- Table structure for table `categories`
  3. --
  4.  
  5. CREATE TABLE `categories` (
  6.   `cat_id` INT(11) NOT NULL,
  7.   `cat_name` VARCHAR(20) NOT NULL
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
For the ProductsTable
  1. --
  2. -- Table structure for table `products`
  3. --
  4.  
  5. CREATE TABLE `products` (
  6.   `product_id` INT(11) NOT NULL,
  7.   `product_name` VARCHAR(50) NOT NULL,
  8.   `cat_id` INT(11) NOT NULL
  9. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Database Connection Using PDO

  1. <?php
  2. $dbcon = new PDO("mysql:host=localhost;dbname=dropDown_test",'root','');
  3. ?>
Codes for the Drop Down List
  1.                 <select id="products_Motor">
  2.                 <option value="showAll" selected="selected">Show All Brands</option>
  3.                 <?php
  4.                 require_once 'config.php';
  5.                
  6.                 $result = $dbcon->prepare('SELECT * FROM categories');
  7.                 $result->execute();
  8.                
  9.                 while($row=$result->fetch(PDO::FETCH_ASSOC))
  10.                 {
  11.                 extract($row);
  12.                 ?>
  13.                 <option value="<?php echo $cat_id; ?>"><?php echo $cat_name; ?></option>
  14.                 <?php
  15.                 }
  16.                 ?>
  17.                 </select>
Codes for the user Select in the Drop Down List
  1. <?php
  2. include('config.php');
  3.  
  4. $action = $_REQUEST['action'];
  5.  
  6. if($action=="showAll"){
  7.        
  8.         $stmt=$dbcon->prepare('SELECT product_id, product_name FROM products ORDER BY product_name');
  9.         $stmt->execute();
  10.        
  11. }else{
  12.        
  13.         $stmt=$dbcon->prepare('SELECT product_id, product_name FROM products WHERE cat_id=:cid ORDER BY product_name');
  14.         $stmt->execute(array(':cid'=>$action));
  15. }
  16. ?>
  17.  
  18. <div class="row">
  19. <?php
  20. if($stmt->rowCount() > 0){
  21. while($row=$stmt->fetch(PDO::FETCH_ASSOC))
  22. {
  23. extract($row);
  24.  
  25. ?>
  26. <div class="col-xs-3">
  27.         <div style="border-radius:10px; border:blue solid 1px; background:azure; color:blue; padding:22px;"><?php echo $product_name; ?></div>
  28.         <br />
  29. </div>
  30. <?php          
  31. }
  32.  
  33. }else{
  34. ?>
  35. <div class="col-xs-3">
  36.         <div style="border-radius:3px; border:#cdcdcd solid 1px; padding:22px;"><?php echo $product_name; ?></div>
  37.         <br />
  38. </div>
  39.  
  40. <?php          
  41. }
  42. ?>
  43. </div>

Output:

The user select in the Drop Down List. Selection After the user select in the Drop Down List. Result Thank you for reading this article. So, this is it, or you can download the full source code below by clicking the "Download Code" button below. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Comments

Add new comment