PHP - Filter And Display Data By Weekly

In this tutorial we will create a Filter And Display Data By Weekly using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. 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_week, 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_week');
  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-widht, 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 - Filter And Display Data By Weekly</h3>
  16.                 <hr style="border-top;1px dotted #ccc;"/>
  17.                 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Stock</button>
  18.                 <br /><br />
  19.                 <form method="POST" class="form-inline" action="">
  20.                         <select name="month" class="form-control" required="required">
  21.                                 <option value="">---Select a month---</option>
  22.                                 <option value="1">January</option>
  23.                                 <option value="2">February</option>
  24.                                 <option value="3">March</option>
  25.                                 <option value="4">April</option>
  26.                                 <option value="5">May</option>
  27.                                 <option value="6">June</option>
  28.                                 <option value="7">July</option>
  29.                                 <option value="8">August</option>
  30.                                 <option value="9">September</option>
  31.                                 <option value="10">October</option>
  32.                                 <option value="11">November</option>
  33.                                 <option value="12">December</option>
  34.                         </select>
  35.                         <select name="year" class="form-control" required="required">
  36.                                 <option value="">---Select a year---</option>
  37.                                 <?php
  38.                                         for($i=date("Y"); $i >= 1965; $i--){
  39.                                                 echo "<option value='".$i."'>".$i."</option>";
  40.                                         }
  41.                                 ?>
  42.                         </select>
  43.                         <button class="btn btn-primary" name="filter"><span class="glyphicon glyphicon-search"></span> Filter</button>
  44.                 </form>
  45.                 <br />
  46.                 <?php include 'get_week.php'?>
  47.         </div>
  48.         <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  49.                 <div class="modal-dialog">
  50.                         <form action="save_stock.php" method="POST" enctype="multipart/form-data">
  51.                                 <div class="modal-content">
  52.                                         <div class="modal-body">
  53.                                                 <div class="col-md-3"></div>
  54.                                                 <div class="col-md-6">
  55.                                                         <div class="form-group">
  56.                                                                 <label>Item Name</label>
  57.                                                                 <input type="text" class="form-control" name="item_name" required="required"/>
  58.                                                         </div>
  59.                                                         <div class="form-group">
  60.                                                                 <label>Brand Name</label>
  61.                                                                 <input type="text" class="form-control" name="brand_name" required="required"/>
  62.                                                         </div>
  63.                                                         <div class="form-group">
  64.                                                                 <label>Date</label>
  65.                                                                 <input type="date" class="form-control" name="date" required="required"/>
  66.                                                         </div>
  67.                                                 </div>
  68.                                         </div>
  69.                                         <div style="clear:both;"></div>
  70.                                         <div class="modal-footer">
  71.                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  72.                                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  73.                                         </div>
  74.                                 </div>
  75.                         </form>
  76.                 </div>
  77.         </div>
  78. <script src="js/jquery-3.2.1.min.js"></script>
  79. <script src="js/bootstrap.js"></script>
  80. </body>
  81. </html>

Creating PHP Query

This code contains the php query of the application. This code will save 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_stock.php.
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['save'])){
  5.                 $item_name = $_POST['item_name'];
  6.                 $brand_name = $_POST['brand_name'];
  7.                 $date = $_POST['date'];
  8.                
  9.                 mysqli_query($conn, "INSERT INTO `stock` VALUES('', '$item_name', '$brand_name', '$date')") or die(mysqli_error());
  10.                 header('location: index.php');
  11.         }
  12. ?>

Creating the Main Function

This code contains the main function of the application. This code will organize the data by week to make it easily to view. To do this just copy and write the code below inside the text editor, then save it as get_week.php.
  1. <?php
  2.         require 'conn.php';
  3.        
  4.         if(ISSET($_POST['filter'])){
  5.                 $month = $_POST['month'];
  6.                 $year = $_POST['year'];
  7.                 $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  8.                
  9.        
  10. ?>
  11.                 <?php
  12.                         echo "<h3>".$months[$month - 1]." ".$year."</h3>";
  13.                 ?>
  14.                 <ul class="nav nav-tabs">
  15.                         <li class="active"><a data-toggle="tab" href="#week1">Week 1</a></li>
  16.                         <li><a data-toggle="tab" href="#week2">Week 2</a></li>
  17.                         <li><a data-toggle="tab" href="#week3">Week 3</a></li>
  18.                         <li><a data-toggle="tab" href="#week4">Week 4</a></li>
  19.                 </ul>
  20.                 <div class="tab-content">
  21.                         <div id="week1" class="tab-pane fade in active" style="padding:10px;">
  22.                                 <table class="table table-bordered">
  23.                                         <thead class="alert-success">
  24.                                                 <tr>
  25.                                                         <th>Item Name</th>
  26.                                                         <th>Brand Name</th>
  27.                                                 </tr>
  28.                                         </thead>
  29.                                         <tbody style="background-color:#fff;">
  30.                                                 <?php
  31.                                                         $data = [];
  32.                                                         $week = [];
  33.                                                         $i = 0;
  34.                                                         $query = mysqli_query($conn, "SELECT WEEK(date) AS `week`, item_name, brand_name FROM `stock` WHERE YEAR(date) = '$year' && MONTH(date) = '$month' ORDER BY `week` ASC") or die(mysqli_error());
  35.                                                         while($fetch = mysqli_fetch_assoc($query)){
  36.                                                                 $week[$i] = $fetch['week'];
  37.                                                                 $data[$i] = array('week' => $fetch['week'],'item_name' => $fetch['item_name'], 'brand_name' => $fetch['brand_name']);
  38.                                                                 $i++;
  39.                                                         }
  40.                                                         $array = array_values(array_unique($week));
  41.                                                         foreach($data as $section => $list){
  42.                                                                 if($list['week'] === $array[0]){
  43.                                                 ?>
  44.                                                         <tr>
  45.                                                                 <td><?php echo $list['item_name']?></td>
  46.                                                                 <td><?php echo $list['brand_name']?></td>
  47.                                                         </tr>
  48.                                                 <?php
  49.                                                                 }
  50.                                                                
  51.                                                         }
  52.                                                
  53.                                                 ?>
  54.                                                
  55.                                         </tbody>
  56.                                 </table>
  57.                         </div>
  58.                         <div id="week2" class="tab-pane fade">
  59.                                 <table class="table table-bordered">
  60.                                         <thead class="alert-success">
  61.                                                 <tr>
  62.                                                         <th>Item Name</th>
  63.                                                         <th>Brand Name</th>
  64.                                                 </tr>
  65.                                         </thead>
  66.                                         <tbody style="background-color:#fff;">
  67.                                                 <?php
  68.                                                         $data = [];
  69.                                                         $week = [];
  70.                                                         $i = 0;
  71.                                                         $query = mysqli_query($conn, "SELECT WEEK(date) AS `week`, item_name, brand_name FROM `stock` WHERE YEAR(date) = '$year' && MONTH(date) = '$month' ORDER BY `week` ASC") or die(mysqli_error());
  72.                                                         while($fetch = mysqli_fetch_assoc($query)){
  73.                                                                 $week[$i] = $fetch['week'];
  74.                                                                 $data[$i] = array('week' => $fetch['week'],'item_name' => $fetch['item_name'], 'brand_name' => $fetch['brand_name']);
  75.                                                                 $i++;
  76.                                                         }
  77.                                                         $array = array_values(array_unique($week));
  78.                                                         foreach($data as $section => $list){
  79.                                                                 if($list['week'] === $array[1]){
  80.                                                 ?>
  81.                                                         <tr>
  82.                                                                 <td><?php echo $list['item_name']?></td>
  83.                                                                 <td><?php echo $list['brand_name']?></td>
  84.                                                         </tr>
  85.                                                 <?php
  86.                                                                 }
  87.                                                                
  88.                                                         }
  89.                                                
  90.                                                 ?>
  91.                                                
  92.                                         </tbody>
  93.                                 </table>
  94.                         </div>
  95.                         <div id="week3" class="tab-pane fade">
  96.                                 <table class="table table-bordered">
  97.                                         <thead class="alert-success">
  98.                                                 <tr>
  99.                                                         <th>Item Name</th>
  100.                                                         <th>Brand Name</th>
  101.                                                 </tr>
  102.                                         </thead>
  103.                                         <tbody style="background-color:#fff;">
  104.                                                 <?php
  105.                                                         $data = [];
  106.                                                         $week = [];
  107.                                                         $i = 0;
  108.                                                         $query = mysqli_query($conn, "SELECT WEEK(date) AS `week`, item_name, brand_name FROM `stock` WHERE YEAR(date) = '$year' && MONTH(date) = '$month' ORDER BY `week` ASC") or die(mysqli_error());
  109.                                                         while($fetch = mysqli_fetch_assoc($query)){
  110.                                                                 $week[$i] = $fetch['week'];
  111.                                                                 $data[$i] = array('week' => $fetch['week'],'item_name' => $fetch['item_name'], 'brand_name' => $fetch['brand_name']);
  112.                                                                 $i++;
  113.                                                         }
  114.                                                         $array = array_values(array_unique($week));
  115.                                                         foreach($data as $section => $list){
  116.                                                                 if($list['week'] === $array[2]){
  117.                                                 ?>
  118.                                                         <tr>
  119.                                                                 <td><?php echo $list['item_name']?></td>
  120.                                                                 <td><?php echo $list['brand_name']?></td>
  121.                                                         </tr>
  122.                                                 <?php
  123.                                                                 }
  124.                                                                
  125.                                                         }
  126.                                                
  127.                                                 ?>
  128.                                                
  129.                                         </tbody>
  130.                                 </table>
  131.                         </div>
  132.                         <div id="week4" class="tab-pane fade">
  133.                                 <table class="table table-bordered">
  134.                                         <thead class="alert-success">
  135.                                                 <tr>
  136.                                                         <th>Item Name</th>
  137.                                                         <th>Brand Name</th>
  138.                                                 </tr>
  139.                                         </thead>
  140.                                         <tbody style="background-color:#fff;">
  141.                                                 <?php
  142.                                                         $data = [];
  143.                                                         $week = [];
  144.                                                         $i = 0;
  145.                                                         $query = mysqli_query($conn, "SELECT WEEK(date) AS `week`, item_name, brand_name FROM `stock` WHERE YEAR(date) = '$year' && MONTH(date) = '$month' ORDER BY `week` ASC") or die(mysqli_error());
  146.                                                         while($fetch = mysqli_fetch_assoc($query)){
  147.                                                                 $week[$i] = $fetch['week'];
  148.                                                                 $data[$i] = array('week' => $fetch['week'],'item_name' => $fetch['item_name'], 'brand_name' => $fetch['brand_name']);
  149.                                                                 $i++;
  150.                                                         }
  151.                                                         $array = array_values(array_unique($week));
  152.                                                         foreach($data as $section => $list){
  153.                                                                 if($list['week'] === $array[3]){
  154.                                                 ?>
  155.                                                         <tr>
  156.                                                                 <td><?php echo $list['item_name']?></td>
  157.                                                                 <td><?php echo $list['brand_name']?></td>
  158.                                                         </tr>
  159.                                                 <?php
  160.                                                                 }
  161.                                                                
  162.                                                         }
  163.                                                
  164.                                                 ?>
  165.                                                
  166.                                         </tbody>
  167.                                 </table>
  168.                         </div>
  169.                 </div>
  170.                
  171.                
  172. <?php
  173.         }else{
  174. ?>
  175.         <table class="table table-bordered">
  176.                         <thead class="alert-success">
  177.                                 <tr>
  178.                                         <th>Item Name</th>
  179.                                         <th>Brand Name</th>
  180.                                         <th>Date</th>
  181.                                 </tr>
  182.                         </thead>
  183.                         <tbody style="background-color:#fff;">
  184.                                 <?php
  185.                                         require 'conn.php';
  186.                                        
  187.                                         $query = mysqli_query($conn, "SELECT * FROM `stock` ORDER BY `date` ASC") or die(mysqli_error());
  188.                                         while($fetch = mysqli_fetch_array($query)){
  189.                                 ?>
  190.                                 <tr>
  191.                                         <td><?php echo $fetch['item_name']?></td>
  192.                                         <td><?php echo $fetch['brand_name']?></td>
  193.                                         <td><?php echo $fetch['date']?></td>
  194.                                 </tr>
  195.                                 <?php
  196.                                         }
  197.                                 ?>
  198.                         </tbody>
  199.                 </table>
  200. <?php
  201.         }
  202. ?>
There you have it we successfully created Filter And Display Data By Weekly 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