简体   繁体   中英

how can i get data depend on user dates

I have a MySQL Datatable
.

I want when user enter date (from-date _ end-date) from HTML form, it will check if the date have data in my sqltable , and bring the result depends in dates that user enter like this

if user inter 10/1/2019 to 16/1/2019 .. it will make HTML form like second picture , start from date [10/1 ** 11/1 ** 12/1 ** 13/1 ** ........ ** 16/1] and check if the first date 10/1 have data in mysql table and bring the result then check the second date 11/1 have data and bring the result ... and so on ,to (end_date 16/1 ).

Ps : the second picture is the actual result I want to show.

If your mysql table has the same structure as on the 1st picture you linked, you just write some php to take start date, look for a records in the database where start date is less or equal to entered start date and end date is greater or equal to the entered start data, and if you find a record, just fetch it and show in html. Then add 1 to the start date, do the steps above and repeat this until the date reaches the entered end date.

PS.: Can you show your code? It could help us a lot.

I created a sample page. Name it to eg. index.php. Change the database connection details and see if it fits your needs. You can change the style with a css, these are for table style:

table{ //complete table

}

th{ //heading style

}

td{ //style of rows
  border: 1px solid black;
  border-collapse: collapse;
}

The php source:

      <html>
      <body>

      <form action = "<?php $_PHP_SELF ?>" method = "GET">
         Hotel: <input type = "text" name = "hotel_id" />
         Start Date: <input type = "date" name = "date" />
         End Date: <input type = "date" name = "end_date" />
         <input type = "submit" />
      </form>

<?php

//if params are not got yet, do not show any table
if( isset($_GET["hotel_id"]) || isset($_GET["date"]) || isset($_GET["end_date"])) {

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = " SELECT * FROM add_allotment WHERE hotel_id = '" . $_GET["hotel_id"] . "' and from_date between '" . $_GET["date"] . "' and '" . $_GET["end_date"] . "' or to_date between '" . $_GET["date"] . "' and '" . $_GET["end_date"] . "' "; 

$result = $conn->query($sql); //run query

$start=strtotime($_GET["date"]); 
$end=strtotime($_GET["end_date"]); 

//echo table beginnings
echo "<table> 
    <tr>
    <th>Date</th>
    <th>Allot</th> 
    <th>over Hotel</th>
    <th>over Agent</th>
    <th>Direct</th> 
    <th>Agent</th>
    <th>Total Rooms</th>
    <th>Booked</th> 
    <th>Remaining</th>
    <th>Chk/in</th>
    <th>Chk/Out</th> 
    <th>In House</th>
    <th>Occupancy</th>
  </tr>";

while($row = $result->fetch_assoc()) { 
//get a row per turn from database, while there is data

for ($seconds=$_GET["date"]; $seconds <= $_GET["end_date"]; $seconds+=86400){
// echo the fetched row for every day
//if Remaining is null, color cell to red, else no color
if($$row[8]==0) {$color="bgcolor=red";} else {$color="";}
//echo the complete row
echo "<table> 
  <tr>
    <td>".$seconds."</td>
    <td>".$row[1]."</td> 
    <td>".$row[2]."</td>
    <td>".$row[3]."</td>
    <td>".$row[4]."</td> 
    <td>".$row[5]."</td>
    <td>".$row[6]."</td>
    <td>".$row[7]."</td> 
    <td".$color.">".$row[8]."</td>
    <td>".$row[9]."</td>
    <td>".$row[10]."</td> 
    <td>".$row[11]."</td>
    <td>".$row[12]."</td>
  </tr>";
}}

echo "</table>"; //close table
}

?>
   </body>
</html>

This code assumes that the data is in the same order as in the second picture. There should not be conflicts with booking intervals in the database. This should be enough for you to start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM