繁体   English   中英

按ID字段对表单结果排序

[英]Sort Form Results by ID Field

以下代码将提交表单中的数据显示到网页上的表格中。 我想按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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM