按字母顺序排列PHP记录



我只是想问,如果我可以按字母顺序排列回声记录是否可以?例如,我想根据他们的名字的第一个字母来安排记录。我还必须添加一些东西才能发生吗?还是可以的?

我知道如何在HTML中按字母顺序排列所有内容,我不确定它是否与PHP相同。

这是我的php代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style>
	body {
		background-image: url("img/wood3.jpg");
	}
	html *
	{
	   margin: auto;
	   color: #000 !important;
	   font-family: Questrial !important;
	}
</style>
	<link href="https://fonts.googleapis.com/css?family=Questrial" rel="stylesheet">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	
<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
	echo "<tr>";
	echo "<td>" . $row->id . "</td>";
	echo "<td>" . $row->name . "</td>";
	echo "<td>" . $row->username . "</td>";
	echo "<td>" . $row->email . "</td>";
	echo "<td>" . $row->address . "</td>";
	echo "<td>" . $row->contact . "</td>";
	echo "<td>" . $row->gender . "</td>";
	echo "<td>" . $row->password . "</td>";
	echo "<td>" . $row->user_levels . "</td>";
	echo "<td>" . $row->date . "</td>";
	echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
	echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
	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>

现在您的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 *),但是在此处的查询中对其进行排序是最有意义的。

*排序和朋友

我认为,在这种情况下,您最简单的方法是从SQL订购,而不是PHP,因此您的查询将成为这样的东西:SELECT * FROM signup_and_login_users_table ORDER BY name DESC

最新更新