I have code in which a user can upload a file/image and text, and then it displays onto the page. I would like for the newest uploaded posts to show first and oldest last, so basically from the bottom up. Here is my code where I am loading it normally:
PHP/SQL:
<?php
$db = mysqli_connect("localhost", "", "", "boxofmem_GMSConnect");
$sql = "SELECT * FROM images";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<p style='font-family:Roboto;color:white;font-size:20px;'>".$row['text']."</p>";
echo "<img src='images/".$row['image']."' ' style='width:100%;height:100%;'>";
echo "</div>";
}
?>
FIELDS:
ID (int:11) IMAGE (BLOB) TEXT (TEXT)
If you mean the reverse order, that’s done in SQL:
SELECT * FROM images ORDER BY something DESC
where something
is the actual field you want to sort. It may be the primary key or a date.
In your case, the ID
column appears to be the primary key, and is probably auto-incremented. That means recent records have higher values. To display the most recent first:
SELECT * FROM images ORDER BY ID DESC
the ORDER BY
clause, which is optional, but always the last clause of an SQL statement, determines the sort order. The default order is ascending ASC
, and is implied if you don’t specify a direction.
DESC
reverses the order. This will bring the most recent to the top.
Add a field to your table called something like recordCreatedWhen. You didn't specify your database engine, but use whatever datatype it has that means date and time
. Give it a default value of the current date and time. Pretty well every database engine out there allows you to do what I just said.
@Manngo already told you about order by
clauses. In this case, it would be
order by recordCreatedWhen desc