假设我有一个如下的数据库结构:
学生
+--------------+--------------------+-------------------+
| student_id | student_firstname | student_lastname |
+--------------+--------------------+-------------------+
| 1 | John | Doe |
+--------------+--------------------+-------------------+
| 2 | Lisa | Doe |
+--------------+--------------------+-------------------+
| 3 | Derp | Doe |
+--------------+--------------------+-------------------+
缺席
+--------------+--------------------+-------------------+---------------+
| absence_id | absence_student_id | absence_date | absence_note |
+--------------+--------------------+-------------------+---------------+
| 1 | 2 | 2012-06-10 | A note... |
+--------------+--------------------+-------------------+---------------+
| 2 | 3 | 2012-06-30 | Another note. |
+--------------+--------------------+-------------------+---------------+
students.student_id
以及列和之间的关系absence.absence_student_id
在 PHP 中,我想遍历上面的表并获得行的完整输出,students
如果有absence
匹配该学生的记录,则对该行执行某些操作,然后继续循环。
我想要的 HTML 输出是这样的:
<table>
<thead>
<tr>
<td>Name:</td>
<td>Absence date:</td>
<td>Absence note:</td>
</tr>
</thead>
<tr>
<td>John Doe</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Lisa Doe</td>
<td>2012-06-10</td>
<td>A note...</td>
</tr>
<tr>
<td>Derp Doe</td>
<td>2012-06-30</td>
<td>Another note.</td>
</tr>
</table>
我用这样的循环:
<?php
while ($row = mysql_fetch_array($students_sql_query)) {
echo "<tr><td>".$row['student_firstname']."</td></tr>";
}
?>
是否可以为此添加某种if
声明?我需要两个不同MySQL
的查询和某种foreach
循环吗?我对网络编程比较陌生...在此先感谢并抱歉发了很长的帖子。