duanchuang3182 2017-09-14 22:08
浏览 86
已采纳

用php排序数据库数据

Newbie here. I am having some trouble with PHP some code. I currently have 1 database, consisting of two tables. The two tables are labeled as members and member_stats. The members table consists of 3 columns (Fname, Lname, MemberID), the member_stats table consists of 11 columns (user_name, first_name, last_name, avatar, num_friends, num_checkins, total_cards, total_selfmade_cards, total_followings, total_photos).

I am fetching the data from my database with

$result = mysqli_query($con,"SELECT * FROM member_stats ");

I am then displaying the data into html table on the page with the following code.

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><div class='user_nav_btn open' ><img src='" . $row[avatar] . "' alt='' /></div></td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['MemberID'] . "</td>";
echo "<td>" . $row['num_friends'] . "</td>";
echo "<td>" . $row['total_cards'] . "</td>";
echo "<td>" . $row['num_checkins'] . "</td>";
echo "<td>" . $row['total_selfmade_cards'] . "</td>";
echo "<td>" . $row['total_followings'] . "</td>";
echo "<td>" . $row['total_photos'] . "</td>";
echo "</tr>";
}

I am trying to get the table to print all the rows of data based on the value of total_cards in order from highest to lowest. No matter what I have tried, I haven’t been able to get it to work. I have only been met with errors on everything I have tried. Any suggestions? I have looked over http://php.net/manual/en/array.sorting.php several times with no luck.

  • 写回答

2条回答 默认 最新

  • duan7264 2017-09-14 22:12
    关注

    As the firt comment drive you on the good way, you don't need PHP sorting as you did, just a Query SQL is sufficient.

    SELECT * FROM member_stats ORDER BY total_cards ASC
    

    By default a SELECT is ASC, i just added for knowing he can do (ASC | DESC) sorting in the Query.

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

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?