簡體   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