gpt4 book ai didi

php - 按 ID 字段对表单结果进行排序

转载 作者:搜寻专家 更新时间:2023-10-30 23:43:48 24 4
gpt4 key购买 nike

以下代码将提交表单中的数据显示到网页上的表格中。我想按 ID 字段的顺序对结果进行排序,最新的条目(因此 ID 编号较高)位于顶部。知道我该怎么做吗?

<?php

try {
$handler = new PDO('mysql:host=localhost;dbname=***', '***', '***');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
die();
}
class guestquestionnaireEntry {
public $id, $date_submitted,
$entry;

public function __construct()
{

$this->entry = "

<table border='1' align='center'>

<tr class='tablelist' style='font-size: 8pt;' ><td width='5%' colspan='2'>{$this->ID}</td><td width='40%' colspan='2'><b>Date Received: </b>{$this->date_submitted}</td><td width='45%' colspan='2'>{$this->name}</td><td width='10%'><a href=\"?ID={$this->ID}\">View here</a> </td></tr>

</table>

";

}

}

SELECT DATE_FORMAT(date_submitted, '%m/%d/%Y') AS date_submitted FROM guestquestionnaire


// Checks if the submitted is a number. If so, isolates the ID and adds "where" clause
$id = (!empty($_GET['ID']) && is_numeric($_GET['ID']))? " where ID = '".$_GET['ID']."'" : "";
// Add the $id to the end of the string
// A single call would be SELECT * FROM guestquestionnaire where ID = '1'
$query = $handler->query("SELECT * FROM guestquestionnaire{$id}");
$query->setFetchMode(PDO::FETCH_CLASS, 'guestquestionnaireEntry');

while($r = $query->fetch()) {
echo $r->entry, '<br>';
}

?>

最佳答案

我假设您的查询完美无缺,但记录未按降序排列。请在您的查询中试试这个

$query   =   $handler->query("SELECT * FROM guestquestionnaire{$id} ORDER BY id DESC");

关于php - 按 ID 字段对表单结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31687685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com