首页 > 解决方案 > 在php中反转数组

问题描述

我正在尝试反转数组,以便它可以先在数据库中显示较新的条目,然后再显示较旧的条目。这是代码:

<?php
    include_once('db/connection.php');
        $query="select * from news";
        $result=mysqli_query($conn,$query);
?>
<?php
    while($rows=mysqli_fetch_array($result))
        {
            ?>
                <div class="container">
                <div class="cust_bloglistintro">
                        <div class="row">
                        <div class="col-md-6 col-lg-4 cust_blogteaser" style="padding-bottom: 20px;margin-bottom: 32px;height: 750px;">
                         <div class="card" data-aos="fade-up" style="height: 700px;"><img class="card-img-top w-100 d-block" src="images/<?php echo $rows['thumbnail'];?>">
                        <div class="card-body">
                                        <h4 class="card-title"><?php echo $rows['title']; ?> </h4><span style="font-family: 'Open Sans', sans-serif;font-size: 12px;margin-bottom: 5px;"><?php echo $rows['date']; ?></span>
                                        <p class="card-text"><?php echo $rows['description']; ?></p><a class="card-link" href="<?php echo $rows['link']; ?>" target="_top">Read more...</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        }
        ?>

标签: phpsql

解决方案


您可以在查询中使用 MySQL 的 ORDER BY 子句。

select * from news ORDER BY id DESC


推荐阅读