2016-06-16 72 views
-1

我有一個PHP代碼,通過從同一頁發送的AJAX請求生成下表。PHP如果語句在取數組中

mysql查詢在「今天」的7天範圍內提取數據。

獲取的數據將被附加到表的tbody中,如下所示。

我的代碼是:

$result = mysqli_query($con," SELECT * FROM `BIMTECH_academy_2016_classes` 
WHERE `RealDate` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY) 
ORDER BY RealDate ASC, RealFrom ASC; "); 

    $html = ""; // a variable 
    while($row = mysqli_fetch_array($result)) 
    { 
     $html.= "<tr>"; 
      $html.= "<td colspan='4' id='date'>" . $row['Date'] . "</td>"; 
     $html.= "</tr>"; 
     $html.= "</tr>"; 
      $html.= "<td>" . $row['CourseName'] . "</td>"; 
      $html.= "<td>" . $row['From'] . "</td>"; 
      $html.= "<td>" . $row['To'] . "</td>"; 
      $html.= "<td>" . $row['InstructorName'] . "</td>"; 
      //$html.= "<td>" . $row['ClassNumber'] . "</td>"; 
     $html.= "</tr>"; 
    } 

enter image description here

結果是正確的,但是,我所要做的是讓重複的日期(S)的紅色如下圖所示的第二個屏幕。

enter image description here

而且我需要添加到表中的其他日子,沒有內容,所以我終於看到七天表(有的用數據,和其他人沒有「關天」)

任何想法?

+0

您可以將數據保存到'陣列什麼()'_(例如)_爲先,然後你可以從這個陣列(的foreach)創建HTML。然後,您可以過濾陣列中的相同日期,並用紅色突出顯示它們。 – pes502

回答

1

應該做ü要

$result = mysqli_query($con," SELECT * FROM `BIMTECH_academy_2016_classes` 
WHERE `RealDate` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY) 
ORDER BY RealDate ASC, RealFrom ASC; "); 

    $html = ""; // a variable 
    $lastDate; 
    while($row = mysqli_fetch_array($result)) 
    { 
     if($lastDate != $row['Date']) 
     { 
       $lastDate = $row['Date']; 
       $html.= "<tr>"; 
        $html.= "<td colspan='4' id='date'>" . $row['Date'] . "</td>"; 
       $html.= "</tr>"; 
     } 
     $html.= "<tr>"; 
      $html.= "<td>" . $row['CourseName'] . "</td>"; 
      $html.= "<td>" . $row['From'] . "</td>"; 
      $html.= "<td>" . $row['To'] . "</td>"; 
      $html.= "<td>" . $row['InstructorName'] . "</td>"; 
      //$html.= "<td>" . $row['ClassNumber'] . "</td>"; 
     $html.= "</tr>"; 
    } 
+0

謝謝@Monarchis,我想實現的另一個目標呢?任何想法 – Mazen

+0

'此外,我需要在表格中添加沒有內容的其他日子,所以我終於在表格中看到了七天(有些是數據,有些則沒有「關閉日期」)' – Mazen