PHP - Display Product Base On Date In SQLite3

In this tutorial we will create a Display Product Base On Date In SQLite3 using PHP. This code will search filter a data in the table when user send a keyword string in the form. The code use PHP POST method to launch a specific function that search filter a specific data in the SQLite database using SELECT query and by adding a conditional statement in the WHERE clause . This a free program, you can apply this to your system as your own. We will be using SQLite that provides an interface for accessing the database. It includes class interfaces to the SQL commands. And also it allows you to create SQL functions and aggregate using PHP.

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/. Installing SQLite Browser We will now then install the SQLite data viewer, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/.

Setting Up SQLite

First, we are going to enable SQLite 3 in our PHP.
  1. Open localhost server folder XAMPP, etc and locate php.ini.
  2. Open php.ini and enable sqlite3 by removing the semicolon in the line. tut1
  3. Save changes and Restart Server.

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=new SQLite3('db/db_product') or die("Unable to open database!");
  3.         $query="CREATE TABLE IF NOT EXISTS `product`(p_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, product_name TEXT, price TEXT, date TEXT)";
  4.  
  5.         $conn->exec($query);
  6. ?>

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. <?php require'conn.php'?>
  3. <html lang="en">
  4.         <head>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  6.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  7.         </head>
  8. <body>
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">PHP - Display Product Base On Date In SQLite3</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-4">
  19.                         <form method="POST" action="save.php">
  20.                                 <div class="form-group">
  21.                                         <label>Product Name</label>
  22.                                         <input type="text" class="form-control" name="product_name" required="required"/>
  23.                                 </div>
  24.                                 <div class="form-group">
  25.                                         <label>Price</label>
  26.                                         <input type="number" min="0" class="form-control" name="price" required="required"/>
  27.                                 </div>
  28.                                 <div class="form-group">
  29.                                         <label>Date</label>
  30.                                         <input type="date" class="form-control" name="date" required="required"/>
  31.                                 </div>
  32.                                 <center><button class="btn btn-primary" name="save">Save</button></center>
  33.                         </form>
  34.                 </div>
  35.                 <div class="col-md-8">
  36.                         <form method="POST" action="">
  37.                                 <div class="form-inline">
  38.                                         <label>Display Row By Date: </label>
  39.                                         <input type="date" class="form-control" name="filter_date" required="required"/>
  40.                                         <button class="btn btn-success" name="filter">Filter</button>
  41.                                 </div>
  42.                         </form>
  43.                         <br />
  44.                         <?php include'filter.php'?>
  45.                 </div>
  46.         </div>
  47. </body>
  48. </html>

Creating the SQLite Query

This code contains the SQLite query of the application. This code will store the data inputs to SQLite database. To do this just copy and write these block of codes as shown below inside the text editor, then save it as save.php.
  1. <?php
  2.         require_once'conn.php';
  3.        
  4.         if(ISSET($_POST['save'])){
  5.                 $product_name=$_POST['product_name'];
  6.                 $price=$_POST['price'];
  7.                 $date=date("Y-m-d", strtotime($_POST['date']));
  8.                
  9.                 $query="INSERT INTO `product` (product_name, price, date) VALUES('$product_name', '$price', '$date')";
  10.  
  11.                 $conn->exec($query);
  12.  
  13.                 header("location:index.php");
  14.         }
  15. ?>

Creating the Main Function

This code contains the main function of the application. This code will search filter a data in the SQLite database. To do this just copy and write these block of codes as shown below inside the text editor, then save it as filter.php.
  1. <?php
  2.         require_once'conn.php';
  3.        
  4.         if(ISSET($_POST['filter'])){
  5.                 $filter_date=$_POST['filter_date'];
  6. ?>
  7.         <table class="table table-bordered">
  8.                 <thead class="alert-info">
  9.                         <tr>
  10.                                 <th>Product Name</th>
  11.                                 <th>Price</th>
  12.                                 <th>Date</th>
  13.                         </tr>
  14.                 </thead>
  15.                 <tbody>
  16.                         <?php      
  17.                                 $query=$conn->query("SELECT * FROM `product` WHERE `date`='$filter_date'") or die("Failed to fetch row!");
  18.                                 while($fetch=$query->fetchArray()){
  19.                                
  20.                         ?>
  21.                                 <tr>
  22.                                 <td><?php echo $fetch['product_name']?></td>
  23.                                 <td><?php echo $fetch['price']?></td>
  24.                                 <td><?php echo $fetch['date']?></td>
  25.                         </tr>
  26.                         <?php
  27.                                 }
  28.                         ?>                     
  29.                 </tbody>
  30.         </table>
  31. <?php
  32. }else{
  33. ?>
  34.         <table class="table table-bordered">
  35.                         <thead class="alert-info">
  36.                                 <tr>
  37.                                         <th>Product Name</th>
  38.                                         <th>Price</th>
  39.                                         <th>Date</th>
  40.                                 </tr>
  41.                         </thead>
  42.                         <tbody>
  43.                                 <?php      
  44.                                         $query=$conn->query("SELECT * FROM `product`") or die("Failed to fetch row!");
  45.                                         while($fetch=$query->fetchArray()){
  46.                                        
  47.                                 ?>
  48.                                 <tr>
  49.                                         <td><?php echo $fetch['product_name']?></td>
  50.                                         <td><?php echo $fetch['price']?></td>
  51.                                         <td><?php echo $fetch['date']?></td>
  52.                                 </tr>
  53.                                 <?php
  54.                                         }
  55.                                 ?>             
  56.                         </tbody>
  57.                 </table>
  58. <?php
  59. }
  60. ?>
There you have it we successfully created a Display Product Base On Date In SQLite3 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