簡體   English   中英

PHP:從MySQL創建數組以構建HTML日歷

[英]PHP: Create array from MySQL to build HTML calendar

目前,我正在為一個小型預訂系統工作,以創建一個已經預訂的時間段的概覽。

預訂信息在我的數據庫中保存如下:

_______________________________________________________________
|id|user_id|     starttime     |      endtime      |facilityID|
---------------------------------------------------------------
|1 |    100|2017-08-21 21:00:00|2017-08-21 22:00:00|        11|
---------------------------------------------------------------

現在,我想構建一個HTML表,顯示當前一天的時間表,還表示所有空閑時隙。

<table>
  <thead>
    <th>Time</th>
    <th>Facility #11</th>
    <th>Facility #12</th>
 </thead>

  <tr>
    <td>08:00</td>
    <td>09:00</td>
    ...
    <td>21:00</td>
    <td>22:00</td>
  </tr>

  <tr>
    <td></td>
    <td></td>
    ...
    <td>User 100</td>
    <td></td>
  </tr>
</table>

Time    Facility #11    Facility #12
08:00   09:00   21:00   22:00
        User 100

為了自動填充該表,我認為創建一個包含所有可能的開始時間作為鍵並將預訂信息作為值(如果存在)的關聯數組是一個好主意。

array(xy) {["08:00"][1]=>string(0) "" [2]=>string(0) "" ["09:00"][1]=>string(0) "" [2]=>string(0) "" ... ["21:00"] [1]=>string(3) "100" [2]=>string(2) "11" ... }

因此,我創建了兩個要合並為一個的數組。

數組1-開始時間

array1 = array();
   for ($hours = 8; $hours <= 22; $hours++) {
      for ($m = 0; $m < 60; $m += 60) {
          $starttime_html = sprintf('%02u:%02u', $hours, $m);
          $array1[$starttime_html]='';

      }
    }

數組2-預訂

array2 = array();
$statement = $pdo->prepare("SELECT id, user_id, starttime, facility_id FROM mpbs_bookings WHERE starttime >= '2017-08-21 00:00:00'");
$statement->execute(array($id));
while($row = $statement->fetch()) {
   $starttime_currentday = explode(" ",$row['starttime']);
   $starttime_curr_hm = substr($starttime_currentday[1],0,5);
   array_push($array2, array($starttime_curr_hm, $row['facility_id'], $row['user_id']));
 }

現在,我找不到組合這兩個數組的方法。 除此之外,對於實現目標的任何建議我都會感到高興。


這是boths數組的內容:

print_r(array1);

Array ( [08:00] => [09:00] => ... => [21:00] => [22:00]=> )


print_r(array2);

Array ( [0] => Array ( [0] => 18:00 [1] => 6 [2] => 22 ) [1] => Array ( [0] => 19:00 [1] => 4 [2] => 11 ) [2] => Array ( [0] => 20:00 [1] => 2 [2] => 5 ) [3] => Array ( [0] => 20:00 [1] => 9 [2] => 8 ) [4] => Array ( [0] => 21:00 [1] => 11 [2] => 34 ) ) 

提前致謝。

拉爾斯

我對Array2的結構進行了更改。 我這樣做是為了可以利用array_merge。 array_merge通過匹配數組鍵並將舊的(數組1)替換為新的(數組2)來工作。

請查看array2的擴展代碼和打印輸出的解決方案

$array2 = array();
$statement = $pdo->prepare("SELECT id, user_id, starttime, facility_id FROM  mpbs_bookings WHERE starttime >= '2017-08-21 00:00:00'");
$statement->execute(array($id));

while($row = $statement->fetch()) {
   $starttime_currentday = explode(" ",$row['starttime']);
   $starttime_curr_hm = substr($starttime_currentday[1],0,5);

   $array2[$starttime_curr_hm] =  
       array('facliId'=>$row['facility_id'],'userid'=> $row['user_id']);
 }

$mergedArray = array_merge($array1,$array2);
foreach($mergedArray as $date=>$value){
   echo $date;
   echo 'Holding facility_id of '.$value['facliId'].' and user_id of '.$value['userid'].'</br>';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM