PHP - Simple Display Data On Current Date

Submitted by: 
Language: 
Visitors have accessed this post 620 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Display Data On Current Date</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form method="POST" action="insert.php">
  19. <div class="form-inline">
  20. <input type="text" name="product_name" class="form-control" placeholder="Product Name" required="required"/>
  21. <input type="date" name="date_purchase" class="form-control" placeholder="Date Purchase" required="required"/>
  22. <button class="btn btn-primary" name="insert">Insert<//button>
  23. </div>
  24. </form>
  25. <br />
  26. <div class="col-md-3"></div>
  27. <div class="col-md-6">
  28. <form method="POST" action="">
  29. <button class="btn btn-success" name="all">Display All</button>
  30. <button class="btn btn-warning" name="current">Display Current</button>
  31. </form>
  32. <br /><br />
  33. <table class="table table-bordered">
  34. <thead class="alert-info">
  35. <tr>
  36. <th>Product Name</th>
  37. <th>Date Purchase</th>
  38. </tr>
  39. </thead>
  40. <?php include'filter.php'?>
  41. </table>
  42. </div>
  43. </div>
  44. </body>
  45. </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. if(ISSET($_POST['all'])){
  3. ?>
  4. <tbody>
  5. <?php
  6. require'conn.php';
  7. $query=mysqli_query($conn, "SELECT * FROM `product`") 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['date_purchase']?></td>
  13. </tr>
  14. <?php
  15. }
  16. ?>
  17. </tbody>
  18. <?php
  19. }else if(ISSET($_POST['current'])){
  20. $date = date("Y-m-d");
  21. ?>
  22. <tbody>
  23. <?php
  24. require'conn.php';
  25. $query=mysqli_query($conn, "SELECT * FROM `product` WHERE `date_purchase`='$date'") or die(mysqli_error());
  26. while($fetch=mysqli_fetch_array($query)){
  27. ?>
  28. <tr>
  29. <td><?php echo $fetch['product_name']?></td>
  30. <td><?php echo $fetch['date_purchase']?></td>
  31. </tr>
  32. <?php
  33. }
  34. ?>
  35. </tbody>
  36. <?php
  37. }else{
  38. ?>
  39. <tbody>
  40. <?php
  41. require'conn.php';
  42. $query=mysqli_query($conn, "SELECT * FROM `product`") or die(mysqli_error());
  43. while($fetch=mysqli_fetch_array($query)){
  44. ?>
  45. <tr>
  46. <td><?php echo $fetch['product_name']?></td>
  47. <td><?php echo $fetch['date_purchase']?></td>
  48. </tr>
  49. <?php
  50. }
  51. ?>
  52. </tbody>
  53. <?php
  54. }
  55. ?>

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.