PHP Using PHP with MySQL Limiting Records in SQL Queries Reversing the Result Set

Yousuf Tafhim
Yousuf Tafhim
8,402 Points

Why apply reverse after we have taken the data from the database in descending order?

In the video we are first writing an SQL Query which return 4 rows in descending order so the records are already coming as 132, 131, 130, 129. But then we are again reversing the order with array_reverse().won't this will make it back to 129, 130, 131, 132?

Yousuf Tafhim
Yousuf Tafhim
8,402 Points

Thanks huanguyen, I was initially thinking that the last product will be the first product on the main page

2 Answers

Changing it back to 129, 130, 131, 132 is to match the intention mentioned in the comment section of the code.

We used DESC to fetch the last 4 shirts, and the order of the fetch is 132, 131, 130, 129.

But in the front end, we want to display the most recent shirts by 129, 130, 131, 132.

Randy wanted to keep this order to maintain display out consistency to keep you having to keep track of which output order is from which block of code.

We can also use ASC and use additional maths to fetch the last four items and not having to use array_reverse, but this is easier.

Aaron Munoz
Aaron Munoz
11,177 Points

Make sure to post the answer in the answer box so people know this question was solved. Thanks Huanguyen