按字母顺序排列PHP中的记录

时间:2017-09-14 14:27:08

标签: php html

我只想问,是否可以按字母顺序排列回声记录?例如,我想按字母顺序根据其名字的第一个字母排列记录。我还要为此添加一些东西吗?或者甚至可能吗?

我知道如何按字母顺序排列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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

我认为在这种情况下,对您来说最简单的方法是从SQL实际订购,而不是PHP,因此您的查询将变为如下所示:SELECT * FROM signup_and_login_users_table ORDER BY name DESC