Jump to content

Fetching array then reversing it


XpenZiF

Recommended Posts

Hi, I have a simple problem, I'll try to explain it as best I can:

News entries in my database are ordered from oldest to newest. In order to pull out the last 10 lines I do this:

$newsquery=mysql_query("SELECT * FROM news ORDER BY timestamp DESC LIMIT 10");
while($row = mysql_fetch_array($newsquery)){
echo '<b>' .$row['line']. '</b><br>';
}

It spits out something like this:

most recent news in db

second most recent news in db

third most recent news in db

etc...

 

The problem is I need to flip that list so the oldest of the 10 is on top and the newest is on bottom.

 

What I've tried:

$row = array_reverse(mysql_fetch_array($newsquery)))

array_reverse didn't seem to do anything. :(

 

I've also read that it can be done with a subquery, but the explanation wasn't that great.

Any help would be much appreciated!

 

 

Link to comment
https://forums.phpfreaks.com/topic/190187-fetching-array-then-reversing-it/
Share on other sites

I guess I worded it wrong.

I wanted the original query's results, just in reverse order. If I change the query to ASC I will get the first 10 lines of my db, which are the 10 oldest news entries.

 

I found a solution shortly after I posted:

 

$results = array();
$newsquery=mysql_query("SELECT * FROM news ORDER BY id DESC LIMIT 10");
while($result = mysql_fetch_assoc($newsquery)){
$results[] = $result;
}
$fixtures = array_reverse($results);
foreach($fixtures as $fixture){
echo '<b>' .$fixture['line']. '</b><br>';
} 

 

I appreciate the responses.

x

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.