Plz frenz help me.
I want to display timetable of college lectures using PHP/MySQL.
I stored the information about lectures in Lectures_Info table. It has following fields:
Subject_Name- name of subject,
Lecture_Start-start timing of lecture(like 09:00),
Lecture_Day-Day of lecture(like Monday,Tuesday).

I attached image for the format of timetable.
There are two arrays for lecture start timings and week days.
I am not getting how to display the subject names in corresponding time and days slot.


So far i tried following code:

<?php $query_schedule = "SELECT Subject_Name, Lecture_Day, Lecture_Start FROM lecture_info WHERE Teacher_Id=$tid";
$schedule = mysql_query($query_schedule, $database) or die(mysql_error());
$row_schedule = mysql_fetch_assoc($schedule);

$lectureStartArray=array('09:00','10:00','11:30','12:30','14:15','15:15');
$weekdays=array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

$subject=array();
$lectureDay=array();
$lectureStart=array();
$i=0;
do{
	  $subject[$i]=$row_schedule['Subject_Name'];
	  $lectureDay[$i]=$row_schedule['Lecture_Day'];
	  $lectureStart[$i]=$row_schedule['Lecture_Start'];
	  $i++;
}while($row_schedule = mysql_fetch_assoc($schedule));
?>


Using these arrays i tried to print subjects as follows:

<table border=1 cellpadding=3>
<tr>
   <td bgcolor="#0099CC"><strong>Hour</strong></td><?php foreach($weekdays as $day) {?>
   <th bgcolor="#FF0066"><strong><?php echo $day;?></strong></th>   
<?php }?>    
</tr>
<?php foreach($lectureStartArray as $val){?>
<tr>
    <td bgcolor="#0099CC"><strong><?php echo $val;?></strong></td><?php foreach($weekdays as $day) {
		$val=$val.':00';	
		 if(in_array($val,$lectureStart)){
			  $i=array_search($val,$lectureStart);	
			  if($day==$lectureDay[$i])	
?>
  	<th bgcolor="#FF99FF"><?php echo $subject[$i];?></th><?php  }
	else?>
    <th bgcolor="#FF9966"></th>
<?php }?>
</tr>
<?php 
}
?>
</table>


But there is something wrong with this code. Plz help me.

Could you post your schema? I can guess it is not well designed. just my guess

Member Avatar for diafol

I agree, storing day as text is awkward - how about daynumber as Monday = 1...Sat = 6.
Also starttime = 9.00etc - what about period = 1 to 6?

If you store like this, you now have 36 periods per week:

You can check for a calculated field, let's call it CELL which can be calculated thus:

"SELECT `period` + (6*(`day` - 1)) AS `cell`, ... ORDER BY `day`,`period`"

during your while loop:

$time = array('09:00','10:00','11:30','12:30','14:15','15:15');
$table = "<table><tr bgcolor=\"#0099CC\"><td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td><td>Friday</td><td>Saturday</td></tr>";
$x=1;$y=0;
while(...){
  if(($x - 1)%6 == 0){
     $table .= "<tr><td bgcolor=\"#0099CC\">{$time[$y]}</td>";
     $y++;
  }
  $table .= ($x == $row['cell'])? "<td>..whatever fields..</td>" : "<td>&nbsp;</td>";
  }
  if($x%6 == 0)$table .= "</tr>";
  $x++;
}
$table .= "</table>";

Anyway, that's just one way. It's pretty inflexible though, unless you change the no_periods and no_days and reformat.

Thanks. It really helped me.

Member Avatar for diafol

ARe we solved?

Not completely but I got idea now. I'll do it. Thanx.

Member Avatar for diafol

BTW, I forgot the 'Hour' heading in the table:

$table = "<table><tr bgcolor=\"#0099CC\"><td bgcolor=\"#0099CC\">Hour</td><td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td><td>Friday</td><td>Saturday</td></tr>";

I got that. I included 'hour' in my code.
:-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.