doufei1988 2017-09-14 14:27
浏览 38
已采纳

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

I just wanted to ask, is it possible if I could arrange the echo records alphabetically? For example, I want to arrange the record based on the first letter of their name alphabetically. Also do I have to add something for that to happen? or is that even possible?

I know how to alphabetically arrange everything in HTML, I'm not sure if it works the same way as PHP.

here's my php code:

<!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>

</div>
  • 写回答

2条回答 默认 最新

  • doujiurong7210 2017-09-14 14:29
    关注

    Right now your SQL query reads:

    SELECT * FROM signup_and_login_users_table ORDER BY id
    

    This means that you sort the results by their numeric id. If you want to sort it by something else, then change the attribute. For example, if you want to sort it by the name:

    SELECT * FROM signup_and_login_users_table ORDER BY name
    

    Or descending order:

    SELECT * FROM signup_and_login_users_table ORDER BY name DESC
    

    You could also sort the results using PHP (with the conveniently named method sort*), but it makes most sense to sort it in the query here.

    * Sort and friends

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 C# TCP服务端,客户端退出后,不断有数据进来
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?