1

我有一张带有用户 ID 和奖励的表格。该用户可以收集不同的奖励,从而为每个奖励获得不同的总价值。见下文:

+-----+-----+-----+-----+-----+
| uid | rw1 | rw2 | rw3 | rw4 |
|  5  |  2  |  6  |  9  |  1  |
+-----+-----+-----+-----+-----+

我正在从我的数据库表中查询它们。如何按从大到小对这些值进行排序?我的用户页面上有一个 div,显示了他们的所有奖励,但我希望最高的奖励首先显示在网页上。我怎样才能做到这一点?不要让它变得更困难,但这些数字是具有图标和名称的更大块元素的一部分。谢谢!

PHP ECHO 转 HTML

$html = '';
$html .='
<div class="reward container id="'.$uid.'">
    <div class="reward-block>
        <div class="reward_icon">
            <img>
        </div>
        <h4>'.rw3.'</div>
    <div>
    <div class="reward-block>
        <div class="reward_icon">
            <img>
        </div>
        <h4>'.rw2.'</div>
    <div>
    <div class="reward-block>
        <div class="reward_icon">
            <img>
        </div>
        <h4>'.rw1.'</div>
    <div>
    <div class="reward-block>
        <div class="reward_icon">
            <img>
        </div>
        <h4>'.rw4.'</div>
    <div>
</div>
';

echo $html;
//echo to HTML 
4

2 回答 2

1

您可以使用ORDER BY

例子:

SELECT
    city,
    first_name,
    last_name
FROM
    sales.customers
ORDER BY
    city DESC,
    first_name ASC;

在上面的示例中,我们根据city降序和first_name升序的值进行排序。

对于您的情况,它将是:

SELECT *
FROM table_name
ORDER BY
   RW3 DESC,
   RW2 DESC,
   RW1 DESC,
   RW4 DESC
于 2020-04-02T23:01:12.810 回答
0

嗨,马克希望你能获得很多有趣的编程。让我帮你解决这个问题。如果您将其复制并粘贴file.php到您xampp htdocs的名称中index.php,将在不到 5 分钟内完成设置。

    <?php

    //

    /*FIRST YOU NEED TO CONNECT TO YOUR DATABASE
    YOU WILL NEED TO REPLACE SOME VALUES, [dbname], [user], [pass] if you are using xampp localhost will be fine if you are using another server manager then use the name that you use to access your data base.*/

    $conn = new PDO("mysql:dbname=dbname;host=localhost;charset=utf8", 'user', 'pass');

    /*
        In the line below we do 2 things first we create a variable called $stm (is a short for statement) and then we use a PDO method (is just a function inside a class and a class is a collection of usefull functions).
        and second we place a query inside the method between quotes '' or ""
    */

//Sorting is done with the ORDER BY followed by the rows you want to sort.
//ASC is for ascending and DESC is for descending sorting

    $stm = $conn->prepare('SELECT * FROM  table ORDER BY rw1, rw2, rw3, rw4 ASC');
    /*THEN WE WILL EXECUTE THE MYSQL QUERY TO GET THE RESULTS*/
    $stm->execute();

    //PACK THIS IN A NICE ARRAY
    $results = (array)$stm->fetchAll();

    ?>

        <!-- AFTER THE ?> THIS SECTION IS HTML -->
        <!-- NOW WE TAKE THE ARRAY AND ONE BY ONE WE CAN PRINT IT IN A UL OR A DIV JUST CHANGE THE HTML TAGS AND YOU'LL BE SET, IF YOU JUST PAST THIS IN A PHP AND REPLACE THE INFO YOU SHOULD GET EVERYTHING UP AND RUNNING IN LES THAN 3 MINUTES-->
        <ul>
            <?php foreach($results as $result): //this is php embedded inline ?>

                    <li>
                        <!-- THEN HERE WE PRINT THEM ONE BY ONE -->
                        <?php echo "{$result['uid']}, {$result['rw1']}, {$result['rw1']}, {$result['rw2']}, {$result['rw3']}, {$result['rw4']}"; //this is php embedded inline?>

                    </li>

            <?php endforeach; //this is php embedded inline ?>
        </ul>

于 2020-04-02T22:41:42.617 回答