echo in reverse order from mysql_fetch_assoc()

后端 未结 3 1917
没有蜡笔的小新
没有蜡笔的小新 2021-01-15 02:13

Ok here\'s the trick. In the query I\'m getting the right results from a table named messages. (Its fetching the last 10 messages ordered by the time inserted in reversed or

相关标签:
3条回答
  • 2021-01-15 02:33

    Store your data & than use array_reverse,

    while($row = mysql_fetch_assoc($result)){
        $items[] = $row;
    }
    
    $items = array_reverse($items ,true);
    
    foreach($items as $item){
       echo $item['time']." - ".$item['username']." - ".$item['message'];
    }
    
    0 讨论(0)
  • 2021-01-15 02:36

    I would use a subquery personally. Im kinda anal about having my data come out of MySQL exactly how I want it, but the array_reverse method will work fine too. Here is my example:

    $query = mysql_query("SELECT * FROM (
                          SELECT time, username, message
                          FROM messages ORDER BY time 
                          DESC LIMIT 10) result 
                          ORDER BY time ASC   
                        ");
    
    0 讨论(0)
  • 2021-01-15 02:44

    Use: array_unshift - Prepend one or more elements to the beginning of an array

    $items = array();
    
    while($row = mysql_fetch_assoc($result)){
        array_unshift($items,$row);
    }
    
    foreach($items as $item){
       echo $item['time']." - ".$item['username']." - ".$item['message'];
    }
    

    Heres a useless but quick example. I hope it's still there: Simple example

    0 讨论(0)
提交回复
热议问题