以倒序方式根据记录添加信息


Adding information based on record in reverse order

我想请求一些帮助。这不是家庭作业,但它只是一个例子,我想如何实现,所以我提供了一个例子。

下面是我的示例代码。

$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

我该怎么做呢?我只遍历记录并显示它,但我不知道如何以倒序添加学生#(旧学生在下面,新学生在上面,以生日为基础)

您应该计算行数,打印并减1

$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>