2

echo "<table class='hoverTable'  cellpadding=10 border=0 align=center style='border-collapse:collapse;width:500px'><tr  style='color:#FFF;background-color:#000; font-size:20px'><td align=center>Date</td><td align=center>Time</td><td align=center colspan=2 >Status</td></tr>";
 for($k=1;$k<=7;$k++)
 {
  if($k==1)
   $day='Monday';
  else if($k==2)
   $day='Tuesday';
  else if($k==3)
   $day='Wednesday';
  else if($k==4)
   $day='Thursday';
  else if($k==5)
   $day='Friday';
  else if($k==6)
   $day='Saturday';
  else 
   $day='Sunday';
  for($i=8; $i<17;$i++)
  {
   $time="$i:00";
   $j= $time+1;
   $endtime="$j:00";

   $sql=mysqli_query($con,"SELECT * FROM (SELECT TT_ID,TT_Subject FROM timetable WHERE TT_Day='$day' and L_ID='00106') AS TAB_1, (SELECT TT_ID,TT_Subject FROM timetable WHERE TT_Time='$time' OR TT_Endtime='$endtime') AS TAB_2 WHERE TAB_1.TT_ID = TAB_2.TT_ID ");
   if(mysqli_num_rows($sql)>0)
   {
    $rows=mysqli_fetch_assoc($sql);
    $sub=$rows['TT_Subject'];
    $runsql=mysqli_query($con,"SELECT * FROM subject WHERE S_Code='$sub'");
    {
     $result=mysqli_fetch_assoc($runsql);
     $subname=$result['S_Name'];
     echo "<tr bgcolor='#F5F6CE'><td>".$day."</td><td align=center style='font-size:15px; width:100px; color:#FF0000;'>".$time." - ".$endtime."</td><td>:</td><td align='center' style='font-size:15px; height:40px; color:#FF0000;' title='Subject: $subname'  >Not Available</td>   ";             
    }
   }
   else
    echo "<tr bgcolor='#F5F6CE'><td>".$day."</td><td align=center style='font-size:15px; width:100px; color: #0000FF;'>".$time." - ".$endtime."</td><td style='width:10px;'>:</td><td align='center' style='font-size:15px; height:40px;'><a href='add_timetable.php'>Available</a></td>";      
  }
  echo "</tr>";
 }

I want to use looping to display the data in database. I success display data but the interface is not I wanted (the table that have color) . Can you please teach me how to display the table like the image below?

<?php
 echo "<table class='hoverTable'  cellpadding=10 border=0 align=center style='border-collapse:collapse;width:500px'><tr  style='color:#FFF;background-color:#000; font-size:20px'><td align=center>Date</td><td align=center>Time</td></tr>";
 for($k=1;$k<=7;$k++)
 {
  if($k==1)
   $day='Monday';
  else if($k==2)
   $day='Tuesday';
  else if($k==3)
   $day='Wednesday';
  else if($k==4)
   $day='Thursday';
  else if($k==5)
   $day='Friday';
  else if($k==6)
   $day='Saturday';
  else 
   $day='Sunday';
  

  for($i=8; $i<17;$i++)
  {
   $time="$i:00";
   $j= $time+1;
   $endtime="$j:00";
   echo "<tr bgcolor='#F5F6CE'><td>".$day."</td><td align=center style='font-size:15px; width:100px; color: #0000FF;'>".$time." - ".$endtime."</td></tr>";      
  }
 
 }
 
?>
enter image description here

enter image description here

enter image description here

wendy
  • 131
  • 7

3 Answers3

2

Of course a lot of ways to approach things. Here's one:

<?php

// SET UP TABLE STRUCTURE
$days = array(
    'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'
);
$daytimes = array(
    array('08:00:00', '09:00:00'), array('09:00:00', '10:00:00')
    , array('10:00:00', '11:00:00'), array('11:00:00', '12:00:00')
    , array('12:00:00', '13:00:00'), array('13:00:00', '14:00:00')
    , array('14:00:00', '15:00:00'), array('15:00:00', '16:00:00')
    , array('16:00:00', '17:00:00'), array('17:00:00', '18:00:00')
);

// SET UP SCHEDULE    
$schedule = array();

// get data for schedule
$sql=mysqli_query($con,"SELECT TT_ID,TT_Subject, TT_Day, TT_Start, TT_Endtime FROM timetable WHERE L_ID='00106'");
if(mysqli_num_rows($sql)>0)
{
  while ($row=mysqli_fetch_assoc($sql)) {
    foreach ( $daytimes as $times ) {
      if ( $row['TT_Start'] == $times[0]
          || $row['TT_Endtime'] == $times[1]
          || ($row['TT_Start'] > $times[0] && $times[1] < $row['TT_Endtime'] )
      ) {
        $schedule["{$times[0]}-{$times[1]}"][$row['TT_Day']][] = $row['TT_Subject'];
      }
    }
  }
}

// DISPLAY SCHEDULE, headers first
echo <<<EOT

<table border="1">
    <tr>
        <td align="center">Time</td><td align="center">Monday</td><td align="center">Tuesday</td><td align="center">Wednesday</td><td align="center">Thursday</td><td align="center">Friday</td><td align="center">Saturday</td><td align="center">Sunday</td>
    </tr>

EOT;

// roll through hours
foreach ( $daytimes as $times ) {
    $timeslot = "{$times[0]}-{$times[1]}";
    echo "<tr><td>$timeslot</td>";

    // roll through days
    foreach ( $days as $day ) {
        echo '<td>';
        // check for subjects in this slot
        if ( isset($schedule[$timeslot][$day]) ) {
            // and display each
            foreach ( $schedule[$timeslot][$day] as $subject ) {
                echo "$subject<br>";
            }
        }
        echo '</td>';
    }
    echo '</tr>';
}
echo '</table>';

?>

First thing here is making one simple db query, rather than 70 complex queries. Then take the db set and roll through it, building a schedule that matches the display.

Then display your table, rolling through the hourly schedule first, then for each hour roll through each day. This provides the information needed to access each slot in the schedule.

This approach also allows multiple subjects in each slot, and subjects to span hours.

This does NOT account for additional subject information in another table or the rest period.

bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
0

The table structure you can design like this the Database related entries you can fill it yourself.

enter image description here

$html = '';
$html .= "<table>";
$html .= "<tr>";
$html .= "<th>Time</th>";
$html .= "<th>Monday</th>";
$html .= "<th>Tuesday</th>";
$html .= "<th>Wednesday</th>";
$html .= "<th>Thursday</th>";
$html .= "<th>Friday</th>";
$html .= "<th>Saturday</th>";
$html .= "<th>Sunday</th>";
$html .= "</tr>";
for($i=8; $i<17;$i++){

    $html .= "<tr>";
    $html .= "<td>".$i.' - ';
    $html .= $i+1;
    $html .= "</td>";
    if($i==13){
        $html .= "<td colspan='7' align='center'> REST </td>";
    }
    else{
        $html .= "<td> </td>";
        $html .= "<td> </td>";
        $html .= "<td> </td>";
        $html .= "<td> </td>";
        $html .= "<td> </td>";
        $html .= "<td> </td>";
        $html .= "<td> </td>";      
    }
    $html .= "</tr>";
}
$html .= '</html>';

echo $html;
Sundar
  • 4,580
  • 6
  • 35
  • 61
  • but i need to save the data into database and display out the data. I have added a picture of my database table. I need to display the TT_Subject on the respective timeslot. I have success display the data by myself. I just want my table to look like that – wendy Jul 26 '15 at 13:02
  • how you fetching the results you didn't update any query. and your display code. i thought you want the structure – Sundar Jul 26 '15 at 13:04
  • you want the result to be displayed in my structure. – Sundar Jul 26 '15 at 13:12
  • yup. Can you teach me? which part of my coding having problem? – wendy Jul 26 '15 at 13:16
  • come to chat. first of tell me in monday only one class or more than one class. if it's only one class we can achieve this if it's more than one we have to redesign the code to display the result ping me on skype sundar.s88 – Sundar Jul 26 '15 at 13:18
  • It will be more than 1 class on a particular day. How to redesign the code?and I have added you in skype. – wendy Jul 26 '15 at 13:28
0

I solved my problem.

<?php
echo "<table class='hoverTable'  cellpadding=10 border=1 align=center style='border-collapse:collapse;width:auto'>";
echo "<tr><td width=50px>Time</td><td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td><td>Friday</td><td>Saturday</td><td>Sunday</td></tr>";      
for($i=8; $i<18;$i++)
{
    echo "<tr>";
    $time="$i:00";
    $j= $time+1;
    $endtime="$j:00";
    echo "<td align=center style='font-size:15px; color:#FF0000;'>".$time." - ".$endtime."</td>";

    if($time=="13:00")
    {
        echo "<td width=80px align='center' style='font-size:15px; height:40px; color:#FF0000;'>Break </td>";
    }
    else
    {
        for($k=1;$k<=7;$k++)
        {
            if($k==1)
                $day='Monday';
            else if($k==2)
                $day='Tuesday';
            else if($k==3)
                $day='Wednesday';
            else if($k==4)
                $day='Thursday';
            else if($k==5)
                $day='Friday';
            else if($k==6)
                $day='Saturday';
            else    
                $day='Sunday';

            echo "<td width=80px align='center' style='font-size:15px; height:40px; color:#FF0000;' >Available</td>";
        }
    }
    echo "</tr>";
}
?>
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
wendy
  • 131
  • 7