我如何获取数据取决于用户日期

时间:2019-01-20 10:18:22

标签: php mysql

我有一个MySQL数据表<​​br/>

我想当用户从HTML表单输入日期(开始日期_结束日期)时,它将检查 如果日期在我的sqltable中有数据,并且带入结果取决于用户输入的日期,例如

如果用户在10/1/2019至16/1/2019之间.. 它将使HTML格式像第二张图片一样,从日期[10/1 ** 11/1 ** 12/1 ** 13/1 ** ........ ** ** 16/1]开始,并检查是否第一个日期10/1在mysql表中有数据并带来结果,然后检查第二个日期11/1有数据并带来结果...依此类推,到(end_date 16/1)。

Ps:第二张图片是我想要显示的实际结果。

2 个答案:

答案 0 :(得分:0)

如果您的mysql表具有与链接的第一张图片相同的结构,则只需编写一些php来获取开始日期,然后在数据库中查找开始日期小于或等于输入的开始日期和结束日期的记录大于或等于输入的起始数据,如果找到一条记录,则将其提取并以html显示。然后将1添加到开始日期,执行上述步骤,并重复该操作直到日期达到输入的结束日期为止。

PS .:您可以显示您的代码吗?这可以对我们有很大帮助。

答案 1 :(得分:0)

我创建了一个示例页面。命名为例如。 index.php。更改数据库连接详细信息,然后查看它是否满足您的需求。您可以使用CSS更改样式,这些样式适用于表格样式:

table{ //complete table

}

th{ //heading style

}

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

php源:

      <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>

此代码假定数据的顺序与第二张图片中的顺序相同。数据库中的预订间隔不应有冲突。这足以让您开始。