PHP 为每个用户循环 HTML 表

标签 php html mysql

我有一个 PHP 循环,它根据 MYSQLi 查询绘制一个表。 我想做的是根据用户 ID 创建此表(在我的例子中,名为“badgeid”的列)

目前正在创建一张表

$sql =  "SELECT first_name,last_name,signintime,signouttime FROM `signin_entries` WHERE iscleaner ='YES' AND signindate = curdate()";
$list_visitors_result=mysqli_query($con,$sql);
$count_visitors = mysqli_num_rows($list_visitors_result);
if($count_visitors != 0) {
    while($row = mysqli_fetch_array($list_visitors_result)){
        $signintime = $row['signintime'];
        $signouttime = $row['signouttime'];
        $firstname = $row['first_name'];
        $lastname = $row['last_name'];
        echo " <tr><td>$firstname $lastname</td>
<td>$signintime</td>";
        if ($signouttime == ""){
            echo "<td>Not Signed Out Yet</td>";
        } else {
            echo "<td>$signouttime</td>";
        }
        $timeFirst  = strtotime(date("Y/m/d") . " " . $signintime);
        $timeSecond = strtotime(date("Y/m/d") ." " . $signouttime);
//below checks if th second time is less than the first time than it must be from the day before so add 24 hours eg (signin time 23:30:00 signout time 07:30:30 would be 08:00:30 difference)
        if ($timeSecond < $timeFirst)
        {
            $timeSecond = $timeSecond + 86400;
        }
        if ($signouttime == ""){
            echo "<td>Cleaner Has Not Signed Out Yet</td>";
        } else {
            $differenceInSeconds = $timeSecond - $timeFirst;
            echo "<td class='rowDataSd'>".converttime($differenceInSeconds)."</td>";
        }
        echo "</tr>";
    }
}
//below function converts the seconds difference into hh:mm:ss format to the nearest second
function converttime($seconds) {
    $t = round($seconds);
    return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
}
echo "<tr><th></th><th></th><th></th><th>Total Time Worked</th><tr><td></td><td></td><td></td><td class='totalCol'>Total:</td></tr>";
?>
</table>
                </div>
              </div>
            </div>
          </div>
          </div>
        </section>
      </div>

这很棒,但我真的需要每个“badgeid”一张 table enter image description here

最佳答案

你可以这样做:

$sql =  "SELECT first_name,last_name,signintime,signouttime FROM `signin_entries` WHERE iscleaner ='YES' AND signindate = curdate() ORDER BY badgeid ASC";
$list_visitors_result=mysqli_query($con, $sql);
$count_visitors = mysqli_num_rows($list_visitors_result);
if ($count_visitors != 0) {
    echo '<table>';
    $current_badgeid = '';
    while ($row = mysqli_fetch_array($list_visitors_result)) {
        if ($current_badgeid == '') {
            $current_badgeid = $row['badgeid']; //define if empty, for the first table
        }
        if($row['badgeid'] != $current_badgeid){
            echo '</table><table>';
            $current_badgeid = $row['badgeid'];
        }
        $signintime = $row['signintime'];
        $signouttime = $row['signouttime'];
        $firstname = $row['first_name'];
        $lastname = $row['last_name'];
        echo " <tr><td>$firstname $lastname</td><td>$signintime</td>";
        if ($signouttime == "") {
            echo "<td>Not Signed Out Yet</td>";
        } else {
            echo "<td>$signouttime</td>";
        }
        $timeFirst  = strtotime(date("Y/m/d") . " " . $signintime);
        $timeSecond = strtotime(date("Y/m/d") ." " . $signouttime);
        //below checks if th second time is less than the first time than it must be from the day before so add 24 hours eg (signin time 23:30:00 signout time 07:30:30 would be 08:00:30 difference)
        if ($timeSecond < $timeFirst) {
            $timeSecond = $timeSecond + 86400;
        }
        if ($signouttime == "") {
            echo "<td>Cleaner Has Not Signed Out Yet</td>";
        } else {
            $differenceInSeconds = $timeSecond - $timeFirst;
            echo "<td class='rowDataSd'>".converttime($differenceInSeconds)."</td>";
        }
        echo "</tr>";
    }
    echo '</table>';
}

首先,将 ORDER BY badidid ASC 添加到查询中。然后,在循环开始之前打开一个表,并定义一个 $current_badgeid var。每次 badid 更改时,您都会关闭并重新打开一个表。

关于PHP 为每个用户循环 HTML 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56936276/

相关文章:

php - Wordpress 自定义联系表不起作用

php - __call() 方法说明

html - 背景图片不会显示在 div 中,但会显示在 body 标签中

html - 使用 BootStrap 侧边栏中的垂直下拉菜单

4 或 5 列上的 MySQL IFNULL

mysql - 如何在 MySQL 查询中汇总每周、每月和每年的金额?

MySQL多次更新错误

PHP/MySQL : Only able to insert values into first row

PHP WebSocket 守护进程

javascript - 尝试让 jQuery 在单击时更改不同的 img src