PHP - Display Data By Monthly

In this tutorial we will create a Display Data By Monthly Using PHP. PHP is a server-side scripting language designed primarily for web development. PHP is a server-side scripting language designed primarily for web development. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_month, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = mysqli_connect('localhost', 'root', '', 'db_month');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="ocntainer-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Display Data By Monthly</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  18.                 <br />
  19.                 <br />
  20.                 <form method="POST" class="form-inline" action="">
  21.                         <select name="date" class="form-control" required="required">
  22.                                 <option value="">---Select an option---</option>
  23.                                 <option value="1">January</option>
  24.                                 <option value="2">February</option>
  25.                                 <option value="3">March</option>
  26.                                 <option value="4">April</option>
  27.                                 <option value="5">May</option>
  28.                                 <option value="6">June</option>
  29.                                 <option value="7">July</option>
  30.                                 <option value="8">August</option>
  31.                                 <option value="9">September</option>
  32.                                 <option value="10">October</option>
  33.                                 <option value="11">November</option>
  34.                                 <option value="12">December</option>
  35.                         </select>
  36.                         <button class="btn btn-primary" name="filter"><span class="glyphicon glyphicon-search"></span> Filter</button>
  37.                 </form>
  38.                 <br />
  39.                 <a class="btn btn-info" href="index.php">Display All</a>
  40.                 <br style="clear:both;"/><br />
  41.                 <table class="table table-bordered">
  42.                         <thead class="alert-info">
  43.                                 <tr>
  44.                                         <th>Product Name</th>
  45.                                         <th>Supplier Name</th>
  46.                                         <th>Date</th>
  47.                                 </tr>
  48.                         </thead>
  49.                         <tbody>
  50.                                 <?php include 'filter.php'?>
  51.                         </tbody>
  52.                 </table>
  53.         </div>
  54.         <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  55.                 <div class="modal-dialog">
  56.                         <form action="save_product.php" method="POST">
  57.                                 <div class="modal-content">
  58.                                         <div class="modal-body">
  59.                                                 <div class="col-md-2"></div>
  60.                                                 <div class="col-md-8">
  61.                                                         <div class="form-group">
  62.                                                                 <label>Product Name</label>
  63.                                                                 <input type="text" class="form-control" name="product_name" required="required"/>
  64.                                                         </div>
  65.                                                         <div class="form-group">
  66.                                                                 <label>Supplier Name</label>
  67.                                                                 <input type="text" class="form-control" name="supplier_name" required="required"/>
  68.                                                         </div>
  69.                                                         <div class="form-group">
  70.                                                                 <label>Date</label>
  71.                                                                 <input type="date" class="form-control" name="date" required="required"/>
  72.                                                         </div>
  73.                                                 </div>
  74.                                         </div>
  75.                                         <div style="clear:both;"></div>
  76.                                         <div class="modal-footer">
  77.                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  78.                                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  79.                                         </div>
  80.                                 </div>
  81.                         </form>
  82.                 </div>
  83.         </div>
  84. <script src="js/jquery-3.2.1.min.js"></script>
  85. <script src="js/bootstrap.js"></script>
  86. </body>
  87. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the data inputs to the database server, To do that just copy and write this block of codes inside the text editor, then save it as save_product.php.
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['save'])){
  5.                 $product_name = $_POST['product_name'];
  6.                 $supplier_name = $_POST['supplier_name'];
  7.                 $date = $_POST['date'];
  8.                
  9.                 mysqli_query($conn, "INSERT INTO `product` VALUES('', '$product_name', '$supplier_name', '$date')") or die(mysqli_error());
  10.                
  11.                 header('location: index.php');
  12.         }
  13.        
  14. ?>

Creating the Main Function

This code contains the main function of the application. This code will filter the record base on month. To do this just copy and write the code inside the text editor, then save it as filter.php.
  1. <?php
  2.         date_default_timezone_set('Asia/Manila');
  3.         if(!ISSET($_POST['filter'])){
  4. ?>
  5.         <?php
  6.                 require 'conn.php';
  7.                 $query = mysqli_query($conn, "SELECT * FROM `product` ORDER BY `date` ASC") or die(mysqli_error());
  8.                 while($fetch = mysqli_fetch_array($query)){
  9.         ?>
  10.                 <tr>
  11.                         <td><?php echo $fetch['product_name']?></td>
  12.                         <td><?php echo $fetch['supplier']?></td>
  13.                         <td><?php echo date('M d, Y', strtotime($fetch['date']))?></td>
  14.                 </tr>
  15.         <?php
  16.                 }
  17.         ?>
  18.        
  19. <?php
  20.         }
  21. ?>
  22.  
  23.  
  24. <?php
  25.         if(ISSET($_POST['filter'])){
  26.                 require 'conn.php';
  27.                 $date = $_POST['date'];
  28.                 $query = mysqli_query($conn, "SELECT * FROM `product` WHERE MONTH(date) = '$date' ORDER BY `date` ASC") or die(mysqli_error());
  29.                 while($fetch = mysqli_fetch_array($query)){
  30. ?>
  31.                 <tr>
  32.                         <td><?php echo $fetch['product_name']?></td>
  33.                         <td><?php echo $fetch['supplier']?></td>
  34.                         <td><?php echo date('M d, Y', strtotime($fetch['date']))?></td>
  35.                 </tr>  
  36. <?php          
  37.                 }
  38.         }
  39. ?>
There you have it we successfully created Display Data By Monthly using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment