I am working on an page that will print scheduled classes and appointments after querying a databse for them. Right now I'm trying to store the names of people with appointments in an array so I can print it after adding the classes as well. I've gotten stuck on this part and was hoping someone knew where the problem is in my code.

$appointments = mysql_query("SELECT * FROM appointment WHERE (teacherid = $id AND weekof = '$weekOf[0]-$weekOf[1]-$weekOf[2]')");
	
	$periods = array('M1'=>'F','M2'=>'F','M3'=>'F','M4'=>'F','M5'=>'F','M6'=>'F','M7'=>'F'
,'M8'=>'F','T1'=>'F','T2'=>'F','T3'=>'F','T4'=>'F','T5'=>'F','T6'=>'F','T7'=>'F','T8'=>'F',
'W1'=>'F','W2'=>'F','W3'=>'F','W4'=>'F','W5'=>'F','W6'=>'F','W7'=>'F','W8'=>'F',
'H1'=>'F','H2'=>'F','H3'=>'F','H4'=>'F','H5'=>'F','H6'=>'F','H7'=>'F','H8'=>'F',
'F1'=>'F','F2'=>'F','F3'=>'F','F4'=>'F','F5'=>'F','F6'=>'F','F7'=>'F','F8'=>'F');

	if (mysql_num_rows($appointments) != 0)
	{	
		while ($row = mysql_fetch_row($appointments))
		{
			foreach ($periods as $key=>$period)
			{
				for ($i = 5; $i < 45; $i++)
				{
					if ($row[$i] == 'T')
					{
						if ($period == 'F')
						{
							$period = $row[$i];
						}
						else
						{
							$period = $period . '<BR />' . $row[$i];
						}
					}
				}
			}
		}
	}

The query returns id, Firstname, Lastname, Teacherid, Weekof, M1,M2,M3... Through F8 (40 total), and Date Created (in that order).
Thanks for any assistance.

Member Avatar for diafol

Here's a modified version of a system I set up a few years back. I use 2 tables - one static timetable and one table called appointments, which is frequently updated. Hope it helps:

$num_periods = 40;
$timetable = mysql_query("SELECT * FROM timetable WHERE teacher_id ='$id'");
//assume 40 periods : id, teacher_id, M1...
$t_data = mysql_fetch_array($timetable);

$appts = mysql("SELECT * FROM appointments WHERE Teacher_id = '$id' AND week = '$whatever'");//assume 40 periods : Firstname, Lastname, Teacherid, Weekof, M1... 
$a_data = mysql_fetch_array($appts);

while($num_periods < 40){
	if($t_data[$num_periods + 2] == '' && $a_data[$num_periods + 4] == ''; //+2 and +4 'coz M1 is field#2 in timetable and field#4 in appts
	 	$placeholder[] = "FREE";
	}elseif($t_data[$num_periods + 2] != '' && $a_data[$num_periods + 4] != ''){
	 	$placeholder[] = "CLASH";
	}elseif($t_data[$num_periods + 2] != ''){
	 	$placeholder[] = $t_data[$num_periods + 2];
	}else{
	 	$placeholder[] = $a_data[$num_periods + 4];
	}
}

$table = "<table border='1'><thead><tr><th>&nbsp;</th><th>M</th><th>T</th><th>W</th><th>H</th><th>F</th></tr></thead><tbody>";

$x = 1;
while($x < 9){
	$y = ($x - 1) * 5;
  	$table.="<tr><td>$x</td><td>{$plceholder[$y]}</td><td>{$plceholder[$y + 1]}</td><td>{$plceholder[$y + 2]}</td><td>{$plceholder[$y + 3]}</td><td>{$plceholder[$y + 4]}</td></tr>";
  	$x++;
}
$table .= "</tbody></table>";


echo $table;
?>

//EDIT
Just realised this version expects a record in the appointments table. You may have to alter the code to avoid an error.

Thanks for that. I had ended up using a bruteforce approach to this. I'll be sure to come back to this when I update the project.

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.