I want to create a student time table having 5 subject, 5 teacher, 5 class, per day 5 hour and 5 day in a week. I have completed everything, but now the issue I am facing is if there is no data inserted for specific one day (suppose for Monday there is no class) and i am inserting data for Tuesday. Then the Tuesday data are showing on Monday.
Here's some images:
Output TimeTable(Here the No Class field's time is 2pm-3pm and day is Thursday. But it is showing in Friday and in 10am-11am)

My TimeTable database table

I want to show if there is no row inserted for a day. Then for that day data should be show as 'No Class' in front end time table. Here I am placing my logic:
<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>
This is my suggestion for you to go with. You can define a new array, and store the values from while loop organised by week day names and then use create another array with name of week days you want to look through, and with foreach loop you can check if you get any value for the day that is in current loop and then print No class for day with no data and time table for the day that have data, look below.
<?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;
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With