我有一个时间表应用程序,它需要根据学期一次性为所有教师生成时间表。我正在查询教师 ID,并且在 while 循环中,我使用了用于生成单个教师时间表的相同代码。根据 mysql 日志,正在为每个教师 ID 触发查询,但正在为所有教师生成相同的时间表。我检查了每一行代码,但它不知何故不起作用。
我很坚持这一点,请帮助。
我的PHP代码:
<?php
include ("includes/functions.php");
$timespan = mysql_real_escape_string($_GET['timespan']);
$q11 = "select * from teachers order by teacher";
$r11 = mysql_query($q11) or die(mysql_error());
while($rows11 = mysql_fetch_array($r11)){
$teacher = $rows11['id'];
$array_day=array('Mon' => 0, 'Tue' => 1, 'Wed' => 2, 'Thu' => 3, 'Fri' => 4, 'Sat' => 5);
if($timespan=="BS"){
$qs="";
}
else if ($timespan=="MS"){
$qs=" and (partno='1' or partno='3' or partno='5' or partno='9') ";
}
else if ($timespan=="WS"){
$qs=" and (partno='2' or partno='4' or partno='6' or partno='9') ";
}
else if ($timespan=="1"){
$qs=" and partno='1' ";
}
else if ($timespan=="2"){
$qs=" and partno='2' ";
}
else if ($timespan=="3"){
$qs=" and partno='3' ";
}
else if ($timespan=="4"){
$qs=" and partno='4' ";
}
else if ($timespan=="5"){
$qs=" and partno='5' ";
}
else if ($timespan=="6"){
$qs=" and partno='6' ";
}
else if ($timespan=="7"){
$qs=" and partno='7' ";
}
else if ($timespan=="8"){
$qs=" and partno='8' ";
}
else if ($timespan=="10"){
$qs=" and partno='1' ";
}
else if ($timespan=="11"){
$qs=" and partno='2' ";
}
else if ($timespan=="9"){
$qs=" and partno='9' ";
}
if($qs==""){
$q1="SELECT tt.day as day, tt.period as period, tt.egstatus as egstat, tt.resp_id as id, tt.sub_group, tt.filter, c.shortname as c_s, p.shortname as p_s, tt.teacherid, tt.subjectid as subject, r.room as room, t.apd1 as apd1, t.apd2 as apd2, g.groupname as class, t.teacher as teacher, p.courseid as course, p.partno as part FROM course c, papers p, ttresponsibility tt, teachers t, groups g, rooms r where c.id=p.courseid and p.paperid = tt.subjectid and tt.teacherid= t.id and tt.room=r.room_id and tt.groupname=g.group_id and tt.teacherid = '$teacher' order by tt.day,tt.period,p.partno ASC";
}else{
$q1="SELECT tt.day as day, tt.period as period, tt.egstatus as egstat, tt.resp_id as id, tt.sub_group, tt.filter, c.shortname as c_s, p.shortname as p_s, tt.teacherid, tt.subjectid as subject, r.room as room, t.apd1 as apd1, t.apd2 as apd2, g.groupname as class, t.teacher as teacher, p.courseid as course, p.partno as part FROM course c, papers p, ttresponsibility tt, teachers t, groups g, rooms r where c.id=p.courseid and p.paperid = tt.subjectid and tt.teacherid= t.id and tt.room=r.room_id and tt.groupname=g.group_id and tt.teacherid = '$teacher'" .$qs. " order by tt.day,tt.period,p.partno ASC";
}
$r1 = mysql_query($q1) or die ("responsibility fetch error:" .mysql_error());
if(mysql_num_rows($r1) > 0){
$q2 = "select teacher,name2,type from teachers where id = '$teacher'";
$r2 = mysql_query($q2) or die("teacher fetch error:" .mysql_error());
$rows1 = mysql_fetch_array($r2);
//$num = mysql_num_rows($r1);
echo "<table width=100% style='border:0px ;font-family:arial;font-size:10px; border- collapse:collapse;cellspacing:0;padding:0px;'><tr><td width=50% align=left border=0 style='border:0px ;font-family:arial;font-size:10px; border-collapse:collapse;cellspacing:0;padding:0px;'>Dated: 31st July 2012</td><td style='border:0px ;font-family:arial;font-size:10px; border-collapse:collapse;cellspacing:0;padding:0px;' width=50% align=right border=0>Version 2.0</td></tr></table>";
echo "<center><font size=5><strong>Shri Ram College of Commerce</strong></font></center>";
echo "<br>";
echo "<table width=100%><tr><td align=left style='border:0px ;font-family:arial;font-size:10px; border-collapse:collapse;cellspacing:0;padding:0px;' ><font size=4><strong>Teacher: ";
if($rows1['type']==1){
echo "".$rows1['name2']."";
}else{
echo "".$rows1['teacher']."";
}
echo "</strong></h3></font></b></td>";
if ($timespan=="MS"){
echo "<td align=right style='border:0px ;font-family:arial;font-size:10px; border-collapse:collapse;cellspacing:0;padding:0px;'><font size=4><strong>Time Span: Monsoon Term</strong></font></br></td></tr></table>";
}
else if ($timespan=="WS"){
echo "<td align=right style='border:0px ;font-family:arial;font-size:10px; border- collapse:collapse;cellspacing:0;padding:0px;'><font size=4><strong>Time Span: Winter Term</strong></font></br></td></tr></table>";
}
echo "<br>";
echo "<table width='100%' border='1' class='tt'>";
echo "<tr class='heads'>";
echo "<td rowspan='2' style='padding:0;text-align:center; width:1%;'> <b>Period<br>Time</b></td>";
echo "<td style='padding:0;text-align:center; '><b>I</b></td>";
echo "<td style='padding:0;text-align:center; '><b>II</b></td>";
echo "<td style='padding:0;text-align:center; '><b>III</b></td>";
echo "<td style='padding:0;text-align:center; '><b>IV</b></td>";
echo "<td style='padding:0;text-align:center; '><b>V</b></td>";
echo "<td style='padding:0;text-align:center; width:1%;' ><b>Break</b></td>";
echo "<td style='padding:0;text-align:center; '><b>VI</b></td>";
echo "<td style='padding:0;text-align:center; '><b>VII</b></td>";
echo "<td style='padding:0;text-align:center; '><b>VIII</b></td>";
echo "<td style='padding:0;text-align:center; '><b>IX</b></td>";
echo "</tr>";
echo "<tr class='heads'>";
echo "<td style='padding:0;text-align:center; '><b>8:40 AM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>9:35 AM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>10:30 AM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>11:25 AM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>12:20 PM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>1:15 PM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>2:00 PM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>2:55 PM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>3:50 PM</b></td>";
echo "<td style='padding:0;text-align:center; '><b>4:45 PM</b></td>";
echo "</tr>";
$k="ToFetchNextRow";
$l = 0;
$rowToProcess=0;
$toRepeat=1;
$num=mysql_num_rows($r1);
$num1=$num-1;
for($i=0;$i<=5;$i++){
$day = array_search($i, $array_day);
echo "<tr class='period'><td style='padding:0;text-align:center;'>
<b>".$day."</b></td>";
for($j=0;$j<=8;$j++){
if($i==0 and $j==5){
echo "<td style='xycell' rowspan=6 > </td>";
}
$noOfEntriesAddedInThisCell=0;
$toRepeat=1;
while($toRepeat==1){
$rows = mysql_fetch_array($r1);
$data[] = $rows; // creating multidimensional array
$dt = $data[$rowToProcess]['day'];
$period = $data[$rowToProcess]['period'];
$paper = $data[$rowToProcess]['p_s'];
$course = $data[$rowToProcess]['c_s'];
$room = $data[$rowToProcess]['room'];
$class = $data[$rowToProcess]['class'];
$id = $data[$rowToProcess]['id'];
$part = $data[$rowToProcess]['part'];
$subgroup = $data[$rowToProcess]['sub_group'];
$weekselector = $data[$rowToProcess]['filter'];
$egstatus = $data[$rowToProcess]['egstat'];
//process the row to be processed
//add the cell entry if it is of interest
if(($i+1) == $dt and ($j+1) == $period){
// check if the slot is busy
if($egstatus == 1){
echo "<td style='xycell' valign=center align=center class='tt'><b><div style='border- bottom:0px solid #bbb;padding:0;font-family:arial;font-size:11px'> <center>Contact<br>Hour</center></td>";
}
elseif($egstatus == 2){
echo "<td style='xycell' valign=center align=center><b><div style='border-bottom:0px solid #bbb;padding:0;font-family:arial;'><a class='slot' style='border-bottom:0px solid #ccc;' href='#' id='status".$dt."+".$period."' onclick='editSlot(".$dt.','.$period.','.$teacher.','.$id."); return false'>Busy</td>";
}
elseif($egstatus == 0){
$e = $i + 1;
$f = $j + 1;
if($noOfEntriesAddedInThisCell==0){
echo "<td valign=center align=center style='border:2px solid #000;padding:0;font-family:arial;'";
if($subgroup!=0){
echo "class='orangeBlock'";
}else{
echo "class='yellowBlock'";
}
echo "><b>";
}
echo "<table class='' style='border:0px solid #000;font-family:arial;border-collapse:collapse;cellspacing:0;padding:0px;'>";
echo "<tr>";
echo "<td style='border-collapse:collapse;border:0px ;font-size:11px; font-family: arial, arial,helvetica, sans-serif;' valign='top'><b> ".$course."-".$part." ".$class."";
if($subgroup!=0){
echo "".$subgroup." ";
}
echo "".$paper." ".$room."";
if($weekselector==1){
echo "(O)</td>";
}
if($weekselector==2){
echo "(E)</td>";
}
echo "</td></tr></table>";
}
$noOfEntriesAddedInThisCell = $noOfEntriesAddedInThisCell+1;
if($rowToProcess < $num1){
$rowToProcess=$rowToProcess+1;
}else{
$toRepeat=0;
}
}else{
if($noOfEntriesAddedInThisCell !=0){
//close the internal table cell
echo "</td>";
}else{
//fill the empty cell
$r = $i + 1;
$s = $j + 1;
echo "<td valign='top' class='tt'><br><br><br></td>";
}
$toRepeat=0;
}
}//while closing
}//jloop closing
}// closing i
echo "</tr>";
echo "</table>";
echo "<br><br><br><br>";
echo "<table width=100% style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;'>";
echo "<tr><td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=left style=font-size:11px> Convenor</td> <td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=center style=font-size:11px>Teacher Incharge</td><td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=right style=font-size:11px>Principal</td></tr>";
echo "<tr><td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=left style=font-size:11px style='border-collapse:collapse;border:0px solid #000;'> Vikas Madan</td><td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=center style=font-size:11px>Amit Sachdeva</td><td style='border:0px ;font-family:arial;font-size:11px; border-collapse:collapse;cellspacing:0;padding:0px;' align=right style=font-size:11px>P. C. Jain</td></tr>";
echo "</table>";
}
else{
echo "";
}
}
?>
编辑的代码片段:
while($toRepeat==1){
while($rows = mysql_fetch_array($r1)){
$data[] = $rows; // creating multidimensional array
$dt = $data[$rowToProcess]['day'];
$period = $data[$rowToProcess]['period'];
$paper = $data[$rowToProcess]['p_s'];
$course = $data[$rowToProcess]['c_s'];
$room = $data[$rowToProcess]['room'];
$class = $data[$rowToProcess]['class'];
$id = $data[$rowToProcess]['id'];
$part = $data[$rowToProcess]['part'];
$subgroup = $data[$rowToProcess]['sub_group'];
$weekselector = $data[$rowToProcess]['filter'];
$egstatus = $data[$rowToProcess]['egstat'];
}
和之后的输出。