我想问一些帮助。这不是作业,但它只是我想要实现的一个例子,所以我提供了一些例子。
这是我的示例代码。
$records = $stmt->prepare("SELECT fname, lname, birthday FROM names ORDER BY birthday DESC");
$records->execute()
foreach($records as $row) {
$birthday = $row['birthday'];
$fname = $row['fname'];
$lname = $row['lname'];
<table>
<tr>
<td>
<?php echo $row['birthday'] . " | " . $row['fname'] . " | " . $row['lname']; ?>
</td>
</tr>
</table>
我想要实现的是在生日前以相反的顺序让学生#。
Student 4 | 1981-11-01 | John | Smith
Student 3 | 1980-11-01 | John | Smith
Student 2 | 1979-11-01 | John | Smith
Student 1 | 1978-11-01 | John | Smith
我该怎么做?我只循环记录并显示它但我不知道如何以相反的顺序添加学生#(下面的老学生和生日顶部的新学生)
非常感谢。
答案 0 :(得分:0)
您应该计算行数,打印和减少一个
$records->execute();
$num_rows = $records->num_rows;
foreach($records as $row) {
$birthday = $row['birthday'];
$fname = $row['fname'];
$lname = $row['lname'];
<table>
<tr>
<td>
<?php echo $num_rows. "|" . $row['birthday'] . " | " . $row['fname'] . " | " . $row['lname'];
$num_rows--;
?>
</td>
</tr>
</table>