我正在尝试使用PHP显示学年表。我似乎行不通,因为学年显得多余。
如何简单地显示它?
No | SY | Student ID | Student Name | Gender | Section Code | Subject ----------------------------------------------------------------------------- 2015-2016 SEMESTER 1 ----------------------------------------------------------------------------- 1 | 2 | 0011941 | Cocos, Scrappy S. | Male | IT15B | IT15 ----------------------------------------------------------------------------- 1 | 2 | 1212211 | asasas, sasas a. | Male | IT15B | IT15 ----------------------------------------------------------------------------- 2015-2016 SEMESTER 2 ----------------------------------------------------------------------------- 1 | 2 | 0011941 | Cocos, Scrappy S. | Male | IT15B | IT15 ----------------------------------------------------------------------------- 1 | 2 | aSAsaSA | dsdsadsad.ssasasas | Male | IT15B | IT15 ----------------------------------------------------------------------------- 2016-2017 SEMESTER 1 ----------------------------------------------------------------------------- 1 | 2 | 0011941 | Doo, Scooby D. | Male | IT15B | IT15 ----------------------------------------------------------------------------- 2016-2017 SEMESTER 2 ----------------------------------------------------------------------------- 1 | 2 | 0011941 | Doo, Scooby D. | Male | IT15B | IT15
这是我的代码:
<tr>
<th>No</th>
<th>SY</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Gender</th>
<th>Section Code</th>
<th>Subject</th>
<th colspan="2">Update</th>
</tr>
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.mi, sts.gender, sub.section_code, sub.subject_name, ets.sem, ets.enroll_num, ets.sy
FROM students sts
JOIN enrollments ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.section_code = ets.section_code)
GROUP BY sts.stud_id, sub.section_code ORDER BY ets.stud_id ASC";
$sql_sel=mysql_query($sql);
$num_rows = mysql_num_rows($sql_sel);
if($num_rows==0)
{
echo "No results found. Please try again";
}
$i=0;
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<th colspan="8"><?php echo $row['sy'];?></th> //this thing here
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['sem'];?></td>
<td><?php echo $row['stud_id'];?></td>
<td width="200"><?php echo $row['lname'].", ".$row['fname']." ".$row['mi'].".";?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['section_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center" width="82"><a href="?tag=enrollment_entry&opr=upd&rs_id=<?php echo $row['enroll_num'];?>" title="Update"><img src="picture/update.png" /></a></td>
<!----<td align="center"><a href="?tag=view_enrollments&opr=del&rs_id=<?php// echo $row['enroll_num'];?>" title="Delete"><img src="picture/delete.png" /></a></td>-------->
</tr>
<?php
}
?>
答案 0 :(得分:1)
首先,您需要在查询中按年份和学期订购:
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.mi, sts.gender, sub.section_code, sub.subject_name, ets.sem, ets.enroll_num, ets.sy
FROM students sts
JOIN enrollments ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.section_code = ets.section_code)
GROUP BY sts.stud_id, sub.section_code ORDER BY ets.sy ASC, ets.sem ASC, ets.stud_id ASC";
然后,当您检测到年度变化时,您只需编写年份行:
$i=0;
$yr=null;
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
if ($yr != ($row['sy'] . ' SEMESTER ' . $row['sem'])) {
$yr = $row['sy'] . ' SEMESTER ' . $row['sem'];
print ('<th colspan="8">' . $yr . '</th>');
}
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['sem'];?></td>
<td><?php echo $row['stud_id'];?></td>
<td width="200"><?php echo $row['lname'].", ".$row['fname']." ".$row['mi'].".";?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['section_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center" width="82"><a href="?tag=enrollment_entry&opr=upd&rs_id=<?php echo $row['enroll_num'];?>" title="Update"><img src="picture/update.png" /></a></td>
<!----<td align="center"><a href="?tag=view_enrollments&opr=del&rs_id=<?php// echo $row['enroll_num'];?>" title="Delete"><img src="picture/delete.png" /></a></td>-------->
</tr>
<?php
}