PHP - Simple Display Data On Current Date

In this tutorial we will create a Simple Display Data On Current Date using PHP. This code can display a data base on the given date when the user submit the form data. The code use MySQLi SELECT query to retrieve the data in a current day when you add WHERE clause in which it contrast the date value. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language that interpret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

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 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_display, 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_display");
  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="continer-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 - Simple Display Data On Current Date</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <form method="POST" action="insert.php">
  18.                         <div class="form-inline">
  19.                                 <input type="text" name="product_name" class="form-control" placeholder="Product Name" required="required"/>
  20.                                 <input type="date" name="date_purchase" class="form-control" placeholder="Date Purchase" required="required"/>
  21.                                 <button class="btn btn-primary" name="insert">Insert<//button>
  22.                         </div>
  23.                 </form>
  24.                 <br />
  25.                 <div class="col-md-3"></div>
  26.                 <div class="col-md-6">
  27.                         <form method="POST" action="">
  28.                                 <button class="btn btn-success" name="all">Display All</button>
  29.                                 <button class="btn btn-warning" name="current">Display Current</button>
  30.                         </form>
  31.                         <br /><br />
  32.                         <table class="table table-bordered">
  33.                                 <thead class="alert-info">
  34.                                         <tr>
  35.                                                 <th>Product Name</th>
  36.                                                 <th>Date Purchase</th>
  37.                                         </tr>
  38.                                 </thead>
  39.                                 <?php include'filter.php'?>
  40.                         </table>
  41.                 </div>
  42.         </div>
  43. </body>
  44. </html>

Creating the PHP Query

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

Creating the Main Function

This code contains the main function of the application. This code will display the data on the current date. To make this just copy and write these block of codes below inside the text editor, then save it as filter.php.
  1. <?php
  2.         date_default_timezone_set("Etc/GMT-8");
  3.         if(ISSET($_POST['all'])){
  4. ?>
  5. <tbody>
  6.         <?php
  7.                 require'conn.php';
  8.                 $query=mysqli_query($conn, "SELECT * FROM `product`") or die(mysqli_error());
  9.                 while($fetch=mysqli_fetch_array($query)){
  10.         ?>
  11.         <tr>
  12.                 <td><?php echo $fetch['product_name']?></td>
  13.                 <td><?php echo $fetch['date_purchase']?></td>
  14.         </tr>
  15.         <?php
  16.                 }
  17.         ?>
  18. </tbody>
  19. <?php          
  20.         }else if(ISSET($_POST['current'])){
  21.         $date = date("Y-m-d");
  22. ?>
  23. <tbody>
  24.         <?php
  25.                 require'conn.php';
  26.                 $query=mysqli_query($conn, "SELECT * FROM `product` WHERE `date_purchase`='$date'") or die(mysqli_error());
  27.                 while($fetch=mysqli_fetch_array($query)){
  28.         ?>
  29.         <tr>
  30.                 <td><?php echo $fetch['product_name']?></td>
  31.                 <td><?php echo $fetch['date_purchase']?></td>
  32.         </tr>
  33.         <?php
  34.                 }
  35.         ?>
  36. </tbody>
  37. <?php
  38.         }else{
  39. ?>
  40. <tbody>
  41.         <?php
  42.                 require'conn.php';
  43.                 $query=mysqli_query($conn, "SELECT * FROM `product`") or die(mysqli_error());
  44.                 while($fetch=mysqli_fetch_array($query)){
  45.         ?>
  46.         <tr>
  47.                 <td><?php echo $fetch['product_name']?></td>
  48.                 <td><?php echo $fetch['date_purchase']?></td>
  49.         </tr>
  50.         <?php
  51.                 }
  52.         ?>
  53. </tbody>
  54. <?php
  55.         }
  56. ?>
There you have it we successfully created Simple Display Data On Current Date 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