且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

每个值都显示在一个新的行 HTML 表中

更新时间:2023-11-30 13:42:10

这里是整个示例:

<?php
    $con = mysqli_connect('localhost', 'root', '', 'test') or die(mysqli_error($con));
    $query = "SELECT * FROM personeel INNER JOIN rooster ON rooster.personeel_id = personeel.id";
    $result = mysqli_query($con, $query) or die(mysqli_error($con));

    if (mysqli_num_rows($result) > 0) {
        $arr = array();
        $nam = array();
        $day = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
        while ($row = mysqli_fetch_assoc($result)) {
            $nam[$row['id']] = $row['naam'];
            $arr[$row['id']][$row['dag']] = $row['start'] . ' - ' . $row['eind'];
        }
        echo '<table border="1">';
            echo '<tr>';
                echo '<td>Employees</td>';
                foreach($day as $d){
                    echo '<td>'.$d.'</td>';
                }
            echo '</tr>';
            foreach ($nam as $k=>$n){
                echo '<tr>';
                    echo '<td>'.$n.'</td>';
                    foreach ($day as $d){
                        if(isset($arr[$k][$d])){
                            echo '<td>'.$arr[$k][$d].'</td>';
                        }else{
                            echo '<td>Virj</td>';
                        }
                    }
                echo '</tr>';
            }
        echo '</table>';
    }
?>

输出为:

一些细节:

$nam 数组存储了从 personeel 中获取的所有姓名以及他的 id 关系.

$nam array stores all fetched names from personeel together with his id relation.

Array
(
    [1] => Tom
    [2] => Jack
)

$arr 是包含人员 ID 和他的数据之间关系的主数组:

$arr is main array which contains relation between personeel id and his data:

Array
(
    [1] => Array
        (
            [Tuesday] => 12:00 - 21:00
            [Wednesday] => 15:00 - 21:00
        )

    [2] => Array
        (
            [Monday] => 08:00 - 18:30
        )

)

$day 是包含工作日的静态数组.

$day is static array with week days.

遍历 $nam$arr 数组,即可获得所需的表,没有任何重复项.另外提供的解决方案使用了 mysqli_* 函数,所以你可以看到如何使用它们.

Walking trough both $nam and $arr arrays gives you desired table without any duplicates. In addition provided solution uses mysqli_* functions, so you can see how to use them.

希望这能帮助您解决问题.:)

Hope this will help you to solve your problem. :)