我目前正在一个小型网站上工作,我的同事可以看到他们的工作时间。
显示:
---------------------------------------------------------------------
|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代码上面的HTML代码:
include_once 'config.php';
$people = "SELECT DISTINCT * FROM personeel INNER JOIN rooster ON rooster.personeel_id = personeel.id";
$result = mysql_query($people);
表格标签内的我的PHP代码:
<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上写下所有的日子,因为那些将有&#34; vrij&#34;作为价值。
但问题是,我希望这样:
------------------------------------------------------
|Employee's|Monday |Tuesday |Wednesday |
|Tom |Vrij |12:00 - 21:00|15:00 - 21:00|
|Jack |08:00 - 18:30|Vrij |Vrij |
------------------------------------------------------
因此,每位员工只有1行,其中列出了所有时间。
我是这个网站的新人,所以我希望我能解释一切顺利,所以你们可以轻松帮助我。
是的,我知道,我使用的是MySQL,因为我想保持简单,我从未使用过MySQLi和PDO。
答案 0 :(得分:0)
这里我们举了整个例子:
<?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关系一起存储。
Array
(
[1] => Tom
[2] => Jack
)
$arr
是主数组,它包含personeel id和他的数据之间的关系:
Array
(
[1] => Array
(
[Tuesday] => 12:00 - 21:00
[Wednesday] => 15:00 - 21:00
)
[2] => Array
(
[Monday] => 08:00 - 18:30
)
)
$day
是带有工作日的静态数组。
通过$nam
和$arr
数组行走,可以为您提供所需的表格,而无需重复。此外,提供的解决方案使用mysqli_*
函数,因此您可以看到如何使用它们。
希望这可以帮助您解决问题。 :)