PHP - Simple Filter Data By Daily

In this tutorial we will create a Simple Filter Data By Daily using PHP. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's 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_daily, 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_daily');
  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 you 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="container-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 Filter Data By Daily</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 /><br />
  19. <form method="POST" class="form-inline" action="">
  20. <label>Select a Day</label>
  21. <input type="date" class="form-control" name="date" required="required"/>
  22. <button class="btn btn-primary" name="filter"><span class="glyphicon glyphicon-search"></span> Filter</button>
  23. <a href="index.php" class="btn btn-warning"><span class="glyphicon glyphicon-refresh"></span></a>
  24. </form>
  25. <br />
  26. <table class="table table-bordered">
  27. <thead class="alert alert-info">
  28. <tr>
  29. <th>Product Name</th>
  30. <th>Supplier Name</th>
  31. <th>Qty</th>
  32. <th>Date Released</th>
  33. </tr>
  34. </thead>
  35. <tbody style="background-color:#fff;">
  36. <?php include 'get_day.php'?>
  37. </tbody>
  38. </table>
  39. </div>
  40. <div class="modal fade" id="form_modal" aria-hidden="true">
  41. <div class="modal-dialog">
  42. <div class="modal-content">
  43. <form method="POST" action="save_product.php">
  44. <div class="modal-header">
  45. <h4 class="modal-title">Add Product</h4>
  46. </div>
  47. <div class="modal-body">
  48. <div class="col-md-2"></div>
  49. <div class="col-md-8">
  50. <div class="form-group">
  51. <label>Product Name</label>
  52. <input type="text" class="form-control" name="product_name" required="required"/>
  53. </div>
  54. <div class="form-group">
  55. <label>Supplier Name</label>
  56. <input type="text" class="form-control" name="supplier" required="required"/>
  57. </div>
  58. <div class="form-group">
  59. <label>Quantity</label>
  60. <input type="number" class="form-control" name="qty" required="required"/>
  61. </div>
  62. <div class="form-group">
  63. <label>Date Released</label>
  64. <input type="date" class="form-control" name="date_released" required="required"/>
  65. </div>
  66. </div>
  67. </div>
  68. <div style="clear:both;"></div>
  69. <div class="modal-footer">
  70. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  71. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  72. </div>
  73. </form>
  74. </div>
  75. </div>
  76. </div>
  77. <script src="js/jquery-3.2.1.min.js"></script>
  78. <script src="js/bootstrap.js"></script>
  79.  
  80. </body>
  81. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the data input 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 = $_POST['supplier'];
  7. $qty = $_POST['qty'];
  8. $date_released = $_POST['date_released'];
  9.  
  10. echo $date_released;
  11.  
  12. mysqli_query($conn, "INSERT INTO `product` VALUES('', '$product_name', '$supplier', '$qty', '$date_released')");
  13. header('location: index.php');
  14. }
  15. ?>

Creating the Main Function

This code contains the main function of the application. This code will filter the data by daily based on the user date input. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. <?php
  2. require 'conn.php';
  3. if(ISSET($_POST['filter'])){
  4. $date = $_POST['date'];
  5. $query = mysqli_query($conn, "SELECT * FROM `product` WHERE `date_released` = '$date' ORDER BY `product_name`") or die(mysqli_error());
  6. while($fetch = mysqli_fetch_array($query)){
  7. ?>
  8. <tr>
  9. <td><?php echo $fetch['product_name']?></td>
  10. <td><?php echo $fetch['supplier']?></td>
  11. <td><?php echo $fetch['qty']?></td>
  12. <td><?php echo date("F d, Y", strtotime($fetch['date_released']))?></td>
  13. </tr>
  14. <?php
  15. }
  16. }else{
  17. ?>
  18. <?php
  19. $query = mysqli_query($conn, "SELECT * FROM `product` ORDER BY `date_released`") or die(mysqli_error());
  20. while($fetch = mysqli_fetch_array($query)){
  21. ?>
  22. <tr>
  23. <td><?php echo $fetch['product_name']?></td>
  24. <td><?php echo $fetch['supplier']?></td>
  25. <td><?php echo $fetch['qty']?></td>
  26. <td><?php echo date("F d, Y", strtotime($fetch['date_released']))?></td>
  27. </tr>
  28. <?php
  29. }
  30. ?>
  31. <?php
  32. }
  33. ?>
There you have it we successfully created Simple Filter Data By Daily 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!

Comments

Add new comment