php - How to display table content in reverse order?
Get the solution ↓↓↓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.
Answer
Solution:
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
Answer
Solution:
Just Modify Your SQL Query
Use ORDER BY
SELECT title, url, photographer, genere, timestamp FROM gallery ORDER BY id DESC ;
Answer
Solution:
If you doesn't want to useORDER BY
:
for($i = count($select_query_result), $i > 0; $i--) {
// Actions
}
Answer
Solution:
UseOrder 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));
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: string literal contains an unescaped line break
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.