每个值都显示在一个新的行 HTML 表中 [英] Each value is displayed in a new row HTML table

查看:30
本文介绍了每个值都显示在一个新的行 HTML 表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在一个小型网站上工作,我的同事可以在其中查看他们的工作时间.

I'm currently working on a small website where my colleagues can see their work times.

  • 我的公鸡"桌子是这样的:
  • rooster_id int(2) Auto_increment 主键.
  • personeel_id int(2)//personeel 在荷兰语中表示工作人员(我正在使用内连接来获取此人的姓名).
  • dag varchar(10)//dag 表示一天.
  • start varchar(5)//start 是员工必须开始的时间工作.
  • eind varchar(5)//eind 表示结束.
  • 基准日期
  • weeknummer int(2)//weeknummer 表示周数.

显示:

---------------------------------------------------------------------
|rooster_id|personeel_id|dag      |start|eind |datum     |weeknummer|
|1         |1           |Tuesday  |12:00|21:00|2016-05-10|19        |
|1         |1           |Wednesday|15:00|21:00|2016-05-11|19        |
|1         |2           |Monday   |08:00|18:30|2016-05-10|19        |
---------------------------------------------------------------------

我在 HTML 标签上方的 php 代码:

My php code above HTML tag:

include_once 'config.php';

$people = "SELECT DISTINCT * FROM personeel INNER JOIN rooster ON rooster.personeel_id = personeel.id";
$result = mysql_query($people);

我在table标签内的php代码:

My php code inside the table tag:

<table cellpadding="1" width="100%" cellspacing="1" class="box-inhoud">
    <?php
    while($row = mysql_fetch_array($result)){
                    echo "<tr>";
                            echo "<td>".$row['naam']."</td>";
                            if($row['dag'] === "maandag"){
                                echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
                            } else {
                                echo "<td class='td-midden'>Vrij</td>";
                            }
                            if($row['dag'] === "dinsdag"){
                                echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
                            } else {
                                echo "<td class='td-midden'>Vrij</td>";
                            }
                            if($row['dag'] === "woensdag"){
                                echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
                            } else {
                                echo "<td class='td-midden'>Vrij</td>";
                            }
                            if($row['dag'] === "donderdag"){
                                echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
                            } else {
                                echo "<td class='td-midden'>Vrij</td>";
                            }
                            if($row['dag'] === "vrijdag"){
                                echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
                            } else {
                                echo "<td class='td-midden'>Vrij</td>";
                            }
                            if($row['dag'] === "zaterdag"){
                                echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
                            } else {
                                echo "<td class='td-midden'>Vrij</td>";
                            }
                            if($row['dag'] === "zondag"){
                                if($row['start'] === "00:00:00"){
                                    echo "<td class='td-midden'>Feestdag</td>";
                                } else {
                                    echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
                                }
                            } else {
                                echo "<td class='td-midden'>Vrij</td>";
                            }
                        echo "</tr>";
                    }
?>
</table>

这会显示这样的数据:

------------------------------------------------------
|Employee's|Monday       |Tuesday      |Wednesday    |
|Tom       |Vrij         |12:00 - 21:00|Vrij         |
|Tom       |Vrij         |Vrij         |15:00 - 21:00|
|Jack      |08:00 - 18:30|Vrij         |Vrij         |
------------------------------------------------------

它还显示星期四、星期五、星期六、星期日,但我不想在 stackoverflow 上写下所有的日子,因为这些日子的值是vrij".

It also displays Thursday, Friday, Saturday, Sunday but I didn't want to write all of the days here on stackoverflow since those will have "vrij" as value.

但问题是,我想要这样:

But the thing is, I want to have it like this:

------------------------------------------------------
|Employee's|Monday       |Tuesday      |Wednesday    |
|Tom       |Vrij         |12:00 - 21:00|15:00 - 21:00|
|Jack      |08:00 - 18:30|Vrij         |Vrij         |
------------------------------------------------------

所以每个员工只有 1 行,其中列出了他们的所有时间.

So every employee does only have 1 row with all their times listed in it.

我是这个网站的新手,所以我希望我解释得很好,所以你们可以轻松地帮助我.

I'm new on this website so I hope I explained everything good, so you guys can easily help me.

是的,我知道,我使用 MySQL 是因为我想保持简单,而且我从未使用过 MySQLi 或 PDO.

And yes I know, I'm using MySQL because I want to keep it simple and I have never worked with MySQLi nor PDO.

推荐答案

这里是整个示例:

<?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. :)

这篇关于每个值都显示在一个新的行 HTML 表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆