我想创建一个学生时间表,每周有5个科目,5个老师,5个班级,每天5小时和5天。我已经完成了所有事情,但现在我面临的问题是,如果没有特定的一天插入数据(假设星期一没有课程),我正在为星期二插入数据。然后星期二的数据显示在星期一。
这里有一些图片:
输出TimeTable(此处No Class字段的时间是下午2点至下午3点,日期是星期四。但它会在星期五和上午10点至11点显示)
我的TimeTable数据库表
我想显示一天是否插入了行。然后在那一天,数据应显示为“No Class'在前端时间表中。 我在这里写下我的逻辑:
<body>
<?php include 'sidenav.php';?>
<article>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<tr>
<th><b>TIME</b></th>
<th><b>Monday</b></th>
<th><b>Tuesday</b></th>
<th><b>Wednesday</b></th>
<th><b>Thursday</b></th>
<th><b>Friday</b></th>
</tr>
<tbody>
<h1>Class Wise Time Table</h1>
<?php
error_reporting(1);
session_start();
if ((isset($_SESSION['login'])) && (isset($_REQUEST['cls']))) {
$classname = $_REQUEST['cls'];
$conn = mysqli_connect("localhost", "root", "", "finaltask");
$sql = "select DISTINCT (time) as time from time_table where classname = "."'$classname'";
$rest = mysqli_query($conn, $sql);
while($t = mysqli_fetch_assoc($rest)){
$sql = "select * from time_table where classname = "."'$classname'". " AND time = '".$t['time']."'";
//print_r($sql);exit;
$res = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($res)){
//echo '<pre>'; print_r ($row);
if(($row['day']=='Monday')&&($row['time']==$t['time'])){
echo"<tr> <td>{$row['time']}</td>";
if(($row['subject']==' ')&&($row['teacher']== ' ')){
echo "<td>No Class</td>";//die('');
}else{
echo "<td>".$row['subject']."<br>".$row['teacher']."</td>";
}
}
if(($row['day']=='Tuesday')&&($row['time'])){
if(($row['subject']=='')&&($row['teacher']=='')){
echo "<td>No Class</td>";
}else{
echo "<td>".$row['subject']."<br>".$row['teacher']."</td>";
}
}
if(($row['day']=='Wednesday')&&($row['time'])){
if(($row['subject']=='')&&($row['teacher']=='')){
echo "<td>No Class</td>";
}else{
echo "<td>".$row['subject']."<br>".$row['teacher']."</td>";
}
}
if(($row['day']=='Thurshday')&&($row['time'])){
if(($row['subject']=='')&&($row['teacher']=='')){
echo "<td>No Class</td>";
}else{
echo "<td>".$row['subject']."<br>".$row['teacher']."</td>";
}
}
if(($row['day']=='Friday')&&($row['time'])){
if(($row['subject']=='')&&($row['teacher']=='')){
echo "<td>No Class</td>";
}else{
echo "<td>".$row['subject']."<br>".$row['teacher']."</td></tr>";
}
}
}
}
}
?>
</tbody>
</table>
答案 0 :(得分:1)
这是我给你的建议。您可以定义一个新数组,并存储由工作日名称组织的while循环中的值,然后使用创建另一个数组,其中包含您想要查看的工作日名称,并使用foreach循环可以检查是否获得了任何值当前循环中的那一天,然后在没有数据的情况下打印No class
一天,在有数据的那天打印time table
,如下所示。
<?php
$days_data = array();
while($row = mysqli_fetch_assoc($res)){
$days_data[$row['day']] == $row;
}
if($days_data):
$week_days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
foreach($week_days as $wday){
if(!empty($days_data[$wday])){
$row_data = $days_data[$wday]; // $row_data contains all of the data from that specific day time,name,teacher and etc...
// there is data for this day
} else {
// no data for the day currently in loop
}
}
endif;
答案 1 :(得分:0)
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<tr>
<th><b>TIME</b></th>
<th><b>Monday</b></th>
<th><b>Tuesday</b></th>
<th><b>Wednesday</b></th>
<th><b>Thursday</b></th>
<th><b>Friday</b></th>
</tr>
<tbody>
<?php
error_reporting(1);
session_start();
if ((isset($_SESSION['login'])) && (isset($_REQUEST['cls']))) {
$classname = $_REQUEST['cls'];
$conn = mysqli_connect("localhost", "root", "", "finaltask");
$sql = "select DISTINCT (time) as time from time_table where classname = "."'$classname'";
$rest = mysqli_query($conn, $sql);
while($t = mysqli_fetch_assoc($rest)){
$sql = "select * from time_table where classname = "."'$classname'". " AND time = '".$t['time']."'";
//print_r($sql);exit;
$res = mysqli_query($conn, $sql);
?>
<tr>
<td><?php echo $t['time'] ?></td>
<?php
$week_days = array('Monday','Tuesday','Wednesday','Thurshday','Friday');
$classes = array();
while($row = mysqli_fetch_assoc($res)) {
$classes[$row['day']] = $row;
}
foreach ($week_days as $day) {
?>
<?php if (array_key_exists($day, $classes)) { $row = $classes[$day]; ?>
<td><?php echo $row['subject'] . '<br />' . $row['teacher'] ?></td>
<?php } else { ?>
<td>No Class</td>
<?php } ?>
<?php
}
?>
</tr>
<?php
}
?>
</tbody>
</table>