2017-09-14 34 views
0

我只是想問一下,如果我可以按字母順序排列回聲記錄,有可能嗎?例如,我想根據他們姓名的首字母按字母順序排列記錄。我也必須添加一些東西來發生?或者甚至有可能?按字母順序排列PHP中的記錄

我知道如何按字母順序排列HTML中的所有內容,我不確定它是否像PHP一樣工作。

這裏是我的PHP代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
 
<html> 
 
<head> 
 

 
<style> 
 

 
\t body { 
 
\t \t background-image: url("img/wood3.jpg"); 
 
\t } 
 

 
\t html * 
 
\t { 
 
\t margin: auto; 
 
\t color: #000 !important; 
 
\t font-family: Questrial !important; 
 
\t } 
 

 
</style> 
 

 
\t <link href="https://fonts.googleapis.com/css?family=Questrial" rel="stylesheet"> 
 
\t <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
 
\t 
 
<title>CORE INTRANET</title> 
 

 
</head> 
 
<body> 
 

 
<br> 
 
<center><h1> View Records </h1></center> 
 
<center><a href="home.php"><img src="img/homebutton.png" height="35" width="35"></a></center> 
 
<br> 
 

 
<?php 
 
// connect to the database 
 
include('connect-db.php'); 
 

 
// get the records from the database 
 
if ($result = $mysqli->query("SELECT * FROM signup_and_login_users_table ORDER BY id")) 
 
{ 
 
// display records if there are records to display 
 
if ($result->num_rows > 0) 
 
{ 
 
// display records in a table 
 
echo "<table border='1' cellpadding='10'>"; 
 

 
// set table headers 
 
echo "<tr><th>ID</th><th>Full Name</th><th>Username</th><th>Email</th><th>Address</th><th>Contact</th><th>Gender</th><th>Password</th><th>Access Level</th><th>Date</th></tr>"; 
 

 
while ($row = $result->fetch_object()) 
 
{ 
 
// set up a row for each record 
 
\t echo "<tr>"; 
 
\t echo "<td>" . $row->id . "</td>"; 
 
\t echo "<td>" . $row->name . "</td>"; 
 
\t echo "<td>" . $row->username . "</td>"; 
 
\t echo "<td>" . $row->email . "</td>"; 
 
\t echo "<td>" . $row->address . "</td>"; 
 
\t echo "<td>" . $row->contact . "</td>"; 
 
\t echo "<td>" . $row->gender . "</td>"; 
 
\t echo "<td>" . $row->password . "</td>"; 
 
\t echo "<td>" . $row->user_levels . "</td>"; 
 
\t echo "<td>" . $row->date . "</td>"; 
 
\t echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>"; 
 
\t echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>"; 
 
\t echo "</tr>"; 
 
} 
 

 
echo "</table>"; 
 
} 
 
// if there are no records in the database, display an alert message 
 
else 
 
{ 
 
echo "No results to display!"; 
 
} 
 
} 
 
// show an error if there is an issue with the database query 
 
else 
 
{ 
 
echo "Error: " . $mysqli->error; 
 
} 
 

 
// close database connection 
 
$mysqli->close(); 
 

 
?> 
 

 
</body> 
 
</html>

回答

2

現在您的SQL查詢寫着:

SELECT * FROM signup_and_login_users_table ORDER BY id 

這意味着,您可以通過他們的數字ID對結果進行排序。如果您想按其他方式對其進行排序,請更改屬性。例如,如果你想通過名稱對它進行排序:

SELECT * FROM signup_and_login_users_table ORDER BY name 

或降序排列:

SELECT * FROM signup_and_login_users_table ORDER BY name DESC 

你也可以使用PHP(與方便的命名方法sort *)對結果進行排序,但在查詢中排序它是最有意義的。

* Sort and friends

+0

我剛剛注意到,我如此愚蠢。謝謝 –

+0

如果解決了您的問題,請接受答案。 – OptimusCrime

+0

我會但我還要等10分鐘。 –

1

我認爲你在這種情況下,最簡單的方法是從SQL命令實際上,不是PHP的,所以您的查詢就會變得像這樣:SELECT * FROM signup_and_login_users_table ORDER BY name DESC