dongzhan2461 2016-02-01 14:23
浏览 8
已采纳

从下到上显示MYSQL表中的项目

I tried to make an app that displays images in a feed form... images from the database are loaded 3 at a time from the top(first) row but i want it to display items in the database from bottom to top since newly inserted items go to bottom of the list... how can I load the table in reverse order? So it displays the rows from the bottom of the database table to the top of the table.

Here is my code... All in the file called "Chat.php"

<?php 
      //Getting the page number which is to be displayed  
      $page = $_GET['page']; 

     //Initially we show the data from 1st row that means the 0th row 
     $start = 0; 

 //Limit is 3 that means we will show 3 items at once
 $limit = 3; 

 //Importing the database connection 
 require_once('dbConnect.php');

 //Counting the total item available in the database 
 $total = mysqli_num_rows(mysqli_query($con, "SELECT id from feed "));

 //We can go atmost to page number total/limit
 $page_limit = $total/$limit; 

 //If the page number is more than the limit we cannot show anything 
 if($page<=$page_limit){

 //Calculating start for every given page number 
 $start = ($page - 1) * $limit; 

 //SQL query to fetch data of a range 
 $sql = "SELECT * from feed limit $start, $limit";

 //Getting result 
 $result = mysqli_query($con,$sql); 

 //Adding results to an array 
 $res = array(); 

 while($row = mysqli_fetch_array($result)){
 array_push($res, array(
 "name"=>$row['name'],
 "publisher"=>$row['publisher'],
 "image"=>$row['image'])
 );
 }
 //Displaying the array in json format 
 echo json_encode($res);
 }else{
            echo "over";
    }
  • 写回答

1条回答 默认 最新

  • douyanqu9722 2016-02-01 14:36
    关注
    //SQL query to fetch data of a range 
     $sql = "SELECT * from feed limit $start, $limit";
    

    update these line like

     //SQL query to fetch data of a range 
     $sql = "SELECT * from feed ORDER BY 'feed .id' DESC limit  $start, $limit";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c