0
我有一張桌子。在表中,有標題customerID,名稱,城市,地址。它們位於表格的頂部,它們是指向同一頁面的鏈接。如何使用鏈接在表格中按字母順序排列並獲取使用PHP和MySQL?
我想要做的是,如果我按下表中的名稱,它將按順序放置列中的所有名稱;與customerID相同,如果我按下鏈接,它會對列中的所有數字進行排序。
我希望你們能幫忙。
<html>
<head>
</head>
<body>
<?php
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND=> 'SET NAMES utf8',
);
$dsn = 'mysql:host=localhost;dbname=name';
$password = 'pass';
$username = 'name';
$db = new PDO($dsn, $username, $password, $options);
$query = "SELECT";
$stmt = $db->prepare($query);
$stmt->execute();
echo "<table border=2>";
echo "<tr>";
echo "<th><a href="mypage.php?sort=customerid">Customer ID: </a></th>";
echo "<th><a href="mypage.php?sort=name">Name:</a></th>";
echo "<th><a href="mypage.php?sort=city">City:</a></th>";
echo "<th><a href="mypage.php?sort=address">Address:</a> </th>";
echo "</tr>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
$customerid =$row['customerid'];
$name =$row['name'];
$city =$row['city'];
$address =$row['address'];
echo "<td>Customer ID: $customerid</td>";
echo "<td>$name</td>";
echo "<td>$city</td>";
echo "<td>$address</td>";
echo "</tr>";
}
echo "</table>";
?>
什麼你要找的是在你按下按鈕,在查詢了'ORDER BY'條款。 – Adrian
@Adrian這就是你的意思?? \t \t \t \t $ query =「SELECT」; ($ _POST ['sort'] =='customerid') } elseif($ _POST ['sort'] =='name') { $ query。=「ORDER BY name」; ($ _POST ['sort'] =='city') { } ($ _ POST ['sort'] =='address') { } $ query。=「ORDER BY address」; } \t \t \t \t $ stmt = $ db-> prepare($ query); \t \t \t \t $ stmt-> execute(); – Steve