簡體   English   中英

如何根據月份並排在兩個HTML表格中顯示日期?

[英]How to display dates in two html tables side by side according to month?

我正在嘗試在給定的應用程序中顯示一個月中每個日期的員工狀態。 我已將數據分為兩個表。 應用快照

現在,如果您看到,則我的表正確顯示了日期。

但是我想每個月開始新表。 即每當新月的第一個日期開始時,我想再次開始渲染該月的日期的過程。

這樣我就可以通過他們的月份來區分他們。

我希望我已經正確解釋了這個問題。 請對此提供指導。 提前致謝。 我的代碼為給定的問題:

<?php       
    $name=$_SESSION['sess_username'];
    $dateofattendance=$_SESSION['sess_date'];
    $time="00-00-00";
    $status="absent";
    $counter=0;
    $conn = new mysqli('localhost', '', '', 'test');
    $sql="SELECT dateofattendance,timeofattendance, status,timeofdeparture FROM attendance Where emp='$name' ORDER BY dateofattendance ASC ";
    $result = $conn->query($sql);           
    if ($result->num_rows > 0)         
        {
            echo "<table class='main'><tbody><tr><td >";
            // create the opening table
            echo "<div align='left'><table class='sep1'style='float:left;'; border='black' cellpadding='5' ><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead>tbody>";
            while($row = $result->fetch_assoc())
                {
                  // create the row
                  echo "<tr><td>" . $row["dateofattendance"]. "</td><td>" . $row["timeofattendance"]. "</td><td>" . $row["timeofdeparture"]. "</td>";
                  if($row["status"]=='present')
                      {
                          echo "<td ><span class='label label-success'>". $row["status"]."</span></td>";
                      }
                  else
                      {
                          echo "<td><span class='label label-danger'>". $row["status"]."</span></td>"; 
                     }"
                </tr>";
            $counter++;  

           // when the counter is 15, close this table and open another
           if($counter == 15)
           {
               echo "</td><td>"; // move to the next cell in our wrap table
               echo "</tbody></table></div>&nbsp;&nbsp;";
               echo "<table class='sep2'style='float:left;'border='black'cellpadding='5'><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead><tbody>";  
           } 

         }
// close the last table
echo "</tbody></table>";

// close our wrapper table
echo "</td></tr></tbody></table>";
}

        $conn->close();
?>

而不是擁有一個計數器並在達到15時創建一個新表,您需要一個變量來存儲呈現的最后一行的月份號,並檢查要呈現的下一行的月份號是否匹配。 如果不匹配,則生成一個新表。

以下代碼產生所需的結果。 我已經用結構相同的數據庫表(名為“ test_employees”的表)對其進行了測試。

注意,代碼輸出有效的HTML,我重新格式化了echo語句並添加了換行符,以使PHP和HTML源代碼更具可讀性。

該代碼可以被大量清理,因此它絕不是好的生產代碼,但再次說明了您所追求的邏輯。

需要注意的一點是,該代碼僅在給定年份內有效。 如果要跨多年顯示表,則需要附加代碼。

如果這對您有幫助,請標記我的答案。

如果您願意,我也可以花更多時間重構此代碼。 讓我知道,下次我會更快答復。

<!DOCTYPE html>
<html>
<head>
<title>table test</title>
</head>
<body>

<?php
    $name=$_SESSION['sess_username'];
    $dateofattendance=$_SESSION['sess_date'];
    $time="00-00-00";
    $status="absent";
    $counter=0;
    $conn = new mysqli('localhost', '', '', 'test_employees');
    $sql="SELECT dateofattendance,timeofattendance, status,timeofdeparture FROM attendance Where emp='$name' ORDER BY dateofattendance ASC ";
    $result = $conn->query($sql);

    if ($result->num_rows > 0)         
    {
        echo "<table border='1'>";
        echo "<thead>";
        echo "<tr>";
        echo "<th>Date</th>";
        echo "<th>IN</th>";
        echo "<th>OUT</th>";
        echo "<th>Status</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";

        $first_iteration = 1;
        $row = $result->fetch_array(MYSQLI_ASSOC);
        $last_month_num = split('-', $row["dateofattendance"])[1];

        while($row = $result->fetch_assoc())
        {
            $current_month_num = split('-', $row["dateofattendance"])[1];

            if ($first_iteration == 1) { // do nothing the first time around
                $first_iteration = 0;
            }
            else if ($current_month_num == $last_month_num) { // only close row in current table
                echo "</tr>";
            }
            else { // close table and start new table
                echo "</tr>";
                echo "</table>";
                echo "<table border='1'>";
                echo "<thead>";
                echo "<tr>";
                echo "<th>Date</th>";
                echo "<th>IN</th>";
                echo "<th>OUT</th>";
                echo "<th>Status</th>";
                echo "</tr>";
                echo "</thead>";
                echo "<tbody>";
            }

            echo "<tr>" . "\n";

            echo "<td>" . "\n" . $row["name"] . $row["dateofattendance"] . "\n" . "</td>" . "\n";
            echo "<td>" . "\n" . $row["timeofattendance"] . "\n" . "</td>" . "\n";
            echo "<td>" . "\n" . $row["timeofdeparture"] . "\n" . "</td>" . "\n";

            if($row["status"]=='present')
            {
                echo "<td><span class='label label-success'>". $row["status"]."</span></td>";
            }
            else
            {
                echo "<td><span class='label label-danger'>". $row["status"]."</span></td>";
            }

            $counter++;

            $last_month_num = split('-', $row["dateofattendance"])[1];
        }

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


    $conn->close();
?>

</body>
</html>

暫無
暫無

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

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