gpt4 book ai didi

php - 如何根据月份并排显示两个html表格中的日期?

转载 作者:行者123 更新时间:2023-11-29 23:07:09 25 4
gpt4 key购买 nike

我正在尝试显示给定应用程序中一个月内每个日期的员工状态。我已将数据分为两个表。 Snapshot of application

现在,如果您可以看到,我的表格正确显示了日期。

但我想每个月开始新的表格。即每当新月的第一个日期开始时,我想再次开始渲染该月日期的过程。

这样我就能通过月份来区分他们。

我希望我已经正确解释了这个问题。请对此提供指导。提前致谢。我针对给定问题的代码:

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

关于php - 如何根据月份并排显示两个html表格中的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28249690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com