如何使用php中的循环选择最接近的日期并显示特定数据?

时间:2018-10-17 11:53:02

标签: php mysqli

使用php和mysql从查询代码显示数据时出现问题。 我想显示最近日期的数据。下面是我要显示的示例数据和输出数据。

Landing Area data.

|landing_id| id_number| address |
+----------+----------+---------+
|    1     | 00012345 | Ozamiz  |
|    2     | 00012346 | Tudela  |
|    3     | 00012347 | Nailon  |
|    4     | 00012348 | Taboo   |
|    5     | 00012349 | Jimenez |
|    6     | 00012350 | Tangub  |
+----------+----------+---------+

Percentage data.

|percent_id| landing_id | percentage |   date_recorded    |
+----------+------------+------------+--------------------+
|    1     |     1      |    19      |2018-10-10 21:42:22 |
|    2     |     1      |    44      |2018-10-16 20:43:32 |
|    3     |     5      |    13      |2018-10-15 19:43:49 |
|    4     |     3      |    22      |2018-10-13 15:43:56 |
|    5     |     3      |    54      |2018-10-12 18:44:03 |
+----------+------------+------------+--------------------+

下面是我的查询和php代码。

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<body>
<?php
include 'db.php';
?>
<table>
    <thead>
        <tr>
          <th ><center>No.</center></th>
          <th ><center>Landing ID</center></th>
          <th><center>Percentage</center></th>
          <th><center>Date Recorded</center></th>
        </tr>
    </thead>
    <tbody>
    <?php
        date_default_timezone_set("Asia/Manila");
        $today = (new DateTime())->format('m-d-Y H:i:s');

        $query = mysqli_query($conn, "SELECT fish_landing.landing_id, 
            percentage.percentage, 
            percentage.date_recorded, 
            fish_landing.address  
            FROM fish_landing 
            LEFT JOIN percentage ON percentage.landing_id = 
            fish_landing.landing_id 
            ORDER BY fish_landing.landing_id ASC");

            $number = 1;
            while ($row = mysqli_fetch_array($query)) {
                echo 
                '<tr>
                   <td><center>' . $number . '</center></td>           
                   <td><center>' . $row['landing_id'] . '</center></td>
                   <td><center>' . $row['percentage'] . '</center></td>
                   <td><center>' . $row['date_recorded'] . '</center></td>
                    </tr>' ;
                    $number++;
                }
                ?>
         </tbody>
      </table>
   </body>
</html>

上面的查询和php代码的输出是这样的,其中必须在表中显示最接近的date_recorded和百分比,但是已经全部显示了一些不最接近date_recorded的数据。

此表将显示在php页面中。

|No.| landing_id | percentage |    date_recorded    |
+----------------+------------+---------------------+
| 1 |     1      |     19     | 2018-10-10 21:42:22 |
| 2 |     1      |     44     | 2018-10-16 20:43:32 |
| 3 |     2      |            |                     |
| 4 |     3      |     54     | 2018-10-12 18:44:03 |
| 5 |     3      |     22     | 2018-10-13 15:43:56 |
| 6 |     4      |            |                     |
| 7 |     5      |     13     | 2018-10-15 19:43:49 |
| 8 |     6      |            |                     |
+----------------+------------+---------------------+

我要显示的输出数据表是下表,将显示每个landing_id中具有最近记录的date_record,而不会显示最早的date_record。

此表将显示在php页面中。

|No.| landing_id | percentage |    date_recorded    |
+----------------+------------+---------------------+
| 1 |     1      |     44     | 2018-10-16 20:43:32 |
| 2 |     2      |            |                     |
| 3 |     3      |     22     | 2018-10-13 15:43:56 |
| 4 |     4      |            |                     |
| 5 |     5      |     13     | 2018-10-15 19:43:49 |
| 6 |     6      |            |                     |
+----------------+------------+---------------------+

我希望有人可以帮助我解决我的问题。预先感谢。

0 个答案:

没有答案