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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度