-2

I need to display table of contents from bottom to top. Since when a new row is inserted, it appears at bottom and when I access it using mysqli_fetch_array it shows the most recent inserted row at the bottom. The code is like this:

<?php
    $abc = mysqli_connect("localhost","root","","members") or die(mysqli_error($abc));
    $select_query = "SELECT title, url, photographer, genere, timestamp FROM gallery";
    $select_query_result = mysqli_query($abc, $select_query) or die(mysqli_error($abc));
?>

And somewhere in html, this code appears.

<ol class="pictures">
    <?php while($row = mysqli_fetch_array($select_query_result)) { ?>
        <li class="thumbnail" data-div="<?php echo $row['genere'] ?>,<?php echo $row['photographer'] ?>,<?php echo $row['title'] ?>,<?php echo $row['timestamp'] ?>" style="background-image: url(<?php echo $row['url'] ?>)">
        </li>
    <?php } ?>
</ol>

So, what should I do to display it in reverse order so that I can get the most recent entry on top while displaying.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anynomous
  • 21
  • 3

4 Answers4

2

Simply order your SQL Query by using either

ORDER BY attribute ASC/DESC

So for example if you want the most recent entry on top, simply change your SQL query to:

"SELECT title, url, photographer, genere, timestamp FROM gallery ORDER BY timestamp desc"

And it should work. You can do this with any attribute you want. I recommend to do it with the primary key (ID) if you have one, but since you're not selecting it, you can do it with your timestamp too. https://www.tutorialspoint.com/sql/sql-sorting-results.htm

Twinfriends
  • 1,972
  • 1
  • 14
  • 34
0

Use Order By:

<?php
        $abc = mysqli_connect("localhost","root","","members") or die(mysqli_error($abc));
        $select_query = "SELECT title, url, photographer, genere, timestamp FROM gallery ORDER BY timestamp DESC";
        $select_query_result = mysqli_query($abc, $select_query) or die(mysqli_error($abc));
        ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Adaleni
  • 966
  • 7
  • 15
0

Just Modify Your SQL Query

Use ORDER BY

SELECT title, url, photographer, genere, timestamp FROM gallery ORDER BY id DESC ;
Aman Kumar
  • 4,533
  • 3
  • 18
  • 40
0

If you doesn't want to use ORDER BY:

for($i = count($select_query_result), $i > 0; $i--) {
    // Actions
}
Nathanael Demacon
  • 1,169
  • 7
  • 19