PHP MySQL Table with a Date Range Filter

As someone who is new to PHP, I have a question. How can I set up a date range filter? I've looked at tutorials, but they haven't worked for me.

I do have some code, but it doesn't include any functions. I want to implement this in a CRUD table.

<form role="form" action="index.php" method="get">
    <div class="form-group">
    <input placeholder="Filter From" class="col-sm-2"  type="text" id="from" name="from">
    <input placeholder="To" class="col-sm-2"  type="text" id="to" name="to">
    <input style="padding-bottom:0px" class="btn btn-default" type="submit" name="go" value="filter range">
    </div>
</form>

and for the date picker, I use the following code.

<script>
$(function(){

    $("#from").datepicker({
        defaultDate:"+lw",
        changeMonth: true,
        numberOfMonths:3,
        onClose:function(selectedDate){
            $("#to").datepicker("option","minDate",selectDate);
        }
    });

    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth:true,
        numberOfMonths: 3,
        onClose: function(selectedDate){
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});
</script>

The PHP table where I want to implement this looks like this:

<?php  
 $connect = mysqli_connect("localhost", "root", "root", "appointments");  
 $output = '';  
 $sql = "SELECT * FROM appointments ORDER BY id DESC";  
 $result = mysqli_query($connect, $sql);  
 $output .= '

      <div class="table-responsive">

           <table class="table table-bordered">  
                <tr>  
                     <th>Select</th>  
                     <th width="10%">Id</th>  
                     <th width="40%">Name</th>  
                     <th width="40%">Email</th>  
                     <th width="40%">Address</th>  
                     <th width="10%">phoneNumber</th>  
                     <th width="10%">appointmentTime</th> 
                     <th width="10%">appointmentDate</th> 
                     <th width="50%">message</th> 
                     <th width="10%">delete</th>

                </tr>';  
                             if(mysqli_num_rows($result) > 0)  
                             {  
                                  while($row = mysqli_fetch_assoc($result))  
                                  {  
                                       $output .= '               

                    <tr id="'.$row["id"].'"> 

                    <td><input type="checkbox" name="id" value="'.$row["id"].'"></td>
                     <td>'.$row["id"].'</td>
                     <td class="name" data-id1="'.$row["id"].'" contenteditable>'.$row["name"].'</td>  
                     <td class="email" data-id2="'.$row["id"].'" contenteditable>'.$row["email"].'</td>
                     <td class="address" data-id2="'.$row["id"].'" contenteditable>'.$row["address"].'</td>  
                     <td class="phoneNumber" data-id2="'.$row["id"].'" contenteditable>'.$row["phoneNumber"].'</td>  
                     <td class="appointmentTime" data-id2="'.$row["id"].'" contenteditable>'.$row["appointmentTime"].'</td>  
                     <td class="appointmentDate" data-id2="'.$row["id"].'" contenteditable>'.$row["appointmentDate"].'</td>
                     <td class="message" data-id2="'.$row["id"].'" contenteditable>'.$row["message"].'</td>
                     <td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger                                               btn_delete">Delete This Row</button></td>
                </tr>  
              ';  
      }                           

 }  
 else  
 {  

      $output .= '<tr><td colspan="10">Data not Found</td></tr>';  
 }  


 $output .= '</table>
<div align="center"> 
<button type="button" name="delete" id="delete">Delete</button>
                </div>  
      </div>   ';  
 echo $output;

 ?>  

Answer №1

It's crucial to obtain the date range value in order to set your appointment dates within that specified range.

Feel free to give this a try!

Here's an example using PHP:

<?php

// Retrieve the date range value
if (isset($_GET['go'])) {

   $date_from   = $_GET['from'];
   $date_to     = $_GET['to'];
}

This is how you can implement it in SQL:

$sql = "SELECT * 
            FROM appointments 
            WHERE appointmentDate 
            BETWEEN  '$date_from' AND '$date_to'
            ORDER BY id 
            DESC"; 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In Javascript, check if an item exists by comparing it to null

I am working with a dropdown list that can be used for various types of data. Some of the data includes an isActive flag (a BOOLEAN) while others do not. When the flag is absent, I would like to display the dropdown item in black. However, if the flag exis ...

Node React authentication

Struggling with implementing authentication in React Router. I am using componentDidMount to check if the user is logged in by calling an endpoint. If not, it should redirect to login, otherwise proceed to the desired component. However, this setup doesn&a ...

What is the best way to ensure that messages stay saved in my app for an extended

I am looking for a way to store messages sent through my small messaging app in a persistent manner. Currently, the messages are transferred from the front-end to a Node, Socket.io, and Express back-end. A friend recommended using Enmaps (), but unfortuna ...

Can you tell me what this code on Facebook is for?

Upon inspection of Facebook's activity in my browser, I stumbled upon the following code snippet: for (;;);{"t":"refresh"} Attempting to decipher its purpose reveals an infinite loop. Can you identify its function? ...

Retrieve a file from an Express API using React with the help of Axios

When working with a React client and an Express API, how can the React client download a file sent by the Express API? Issue: If I manually enter the URL into my browser's address bar and hit enter, the file downloads successfully. However, when I ...

Applying the document height to window height ratio to create a scale ranging from 1 to 100

My goal is to create a scroll bar using two divs with heights of 110px and 10px. The smaller one will be nested inside the taller one, allowing for adjustment of its margin-height from 0 to 100px while still fitting within the larger div. When I refer to ...

Exploring jQuery Mobile - What Causes an Empty State?

Using $.mobile.navigate("#test-page", {id:123}) for navigation to a secondary page seems to be successful. The transition between pages is smooth.... but the state remains empty! According to the documentation, the state should contain all necessary info ...

Maintaining header and footer while calling a PHP form on the same page

I have a straightforward PHP form, named home.php, that submits to itself: <form action="home.php" method="post" enctype="multipart/form-data" id="AddImageForm"> The issue arises when home.php is accessed from the index.php file (located below). Up ...

How to retrieve the filename from a URL using Node.js

In my Node.js script, I have the following type of link: 148414929_307508464041827_8013797938118488137_n.mp4.m4a?_nc_ht=scontent-mxp1-1.cdninstagram.com&_nc_ohc=_--i1eVUUXoAX9lJQ-u&ccb=7-4&oe=60835C8D&oh=61973532a48cb4fb62ac6711e7eba82f& ...

Using the dot operator to load an image

Is it possible to using the dot operator to load an image? Let's talk about this as a sample image URL <img src={{GET_PROFILE_DATA.googleProfileData.fullName}} alt="profile" class="home-screen-profile-image"> Take note of the unusual looking ...

Guide on plotting latitude and longitude coordinates on Google Maps with Vue.js through JSON data fetched via AJAX

I have implemented a Vue.js script to fetch JSON data through an AJAX request. However, I am encountering issues with the code. <script> new Vue({ el: '#feed' , data: { details: [], }, mounted() { this.$nextTick(fu ...

Having trouble parsing JSON data from $_POST while utilizing AngularJS

While working in Angular, I encountered the following scenario: var data = {}; data['name'] = "test"; data['class'] = "testClass"; $http.post('http://testURL',data,function(data){ console.log(data); }); Upon inspecting the ...

No data found in php://input when accessing Azure

Having an issue with posting a complex JSON object to my Azure IIS server running PHP 5.4 using AngularJS: var request = $http({ method: "post", url: "/php/mail.php", data: $scope.contactForm, headers: { 'Content-Type': & ...

The Mystery of Socket.io Random Disconnects (version 1.0.6)

Currently, I am utilizing the most recent version of socket.io (1.0.6) to develop an online multiplayer game using Phaser and Node. One issue that has arisen is that after the clients connect, they will sporadically disconnect without any specific pattern. ...

A guide on how to automatically preselect a RadioGroup option in Material-UI

When a user selects an option from the MCQ Select using RadioGroup in my code and submits it, they should be able to return later and see the option they selected highlighted, similar to how Google Forms allows users to review their selections. Below is t ...

React is displaying [object Object] instead of the intended value on the webpage. What steps can be taken to resolve this issue?

I have attempted to retrieve data from an API and am currently working on displaying this data within a table cell inside a component. Instead of rendering the original data, React is displaying [object Object]. I tried using the join() method with commas ...

Why is it that my JQuery sliders appear perfectly fine when viewed locally, but fail to show up when accessed on

I'm facing an issue with two JQuery sliders on my page. The local version works fine, but when I upload it to my web host, only one slider functions correctly. I need both of them to work as intended. Any insights on how to resolve this problem? See ...

Obtaining a cookie for the PHPUnit test involving Ajax functionality

When it comes to writing PHPUnit tests for ajax endpoints, I find it easy when the user doesn't need to be logged in. However, for the specific endpoint I want to test, the user must be logged in and I need to programmatically retrieve the cookie as p ...

Sharing tips for sending error objects to a socket.io callback

Utilizing callbacks with socket.io Client side code : socket.emit('someEvent', {data:1}, function(err, result) { console.log(err.message); }); Server side code : socket.on('someEvent', function(data, callback) { callback(ne ...

Encountered a SyntaxError in vue + webpack regarding an invalid range in character class

I am currently using webpack 4.29.3 and vue.js 2.6.3 to create a simple hello world project. I expected the index.html file to render correctly, but I encountered an error: SyntaxError: invalid range in character class. This error is confusing because I&ap ...