[SOLVED] Reversing results.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

[SOLVED] Reversing results.

Post by jslick »

Let's say I have:

Code: Select all

$query="SELECT * FROM news_posts";
$result=mysql_query($query,$link);
$numrows=mysql_num_rows($result);
for($i=0; $i<$numrows; $i++){
$the_arrays=mysql_fetch_array($result);
print("<br /><h2 class="sub">{$the_arrays['title']}</h2>\n");
print("<br /><span style="font-size:8pt;">posted: <i>{$the_arrays['date']}</i>"
."\nID: <i>{$the_arrays['id']}</i></span>\n");
print("<br /><p>{$the_arrays['posttext']}</p>");
}
How could I reverse the results?

[Edit: Fixed tags. --JAM]
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

1) Add an ORDER BY into the sql query.
2) Get the results into an array and use array_reverse().
3) Store what your eching into a string and add the new results to the start of the string instead of the end - so the last result is first in the string:

Code: Select all

$text = "new results here blah blah blah".$text;
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post by jslick »

Thanks kettle_drum! I used the ORDER BY to get it working.
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

Hi,

Would it be possible to see the code that worked in this situation? I have a similar situation on the back burner. It's there because I couldn't figure out how to get it to work. :cry:

Thanks
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post by jslick »

Sure...

Code: Select all

<?php
// code left out above...reply if you need it (I kind of doubt you'll need it, though)
$query="SELECT * FROM news_posts ORDER BY id DESC";
$result=mysql_query($query,$link);
$numrows=mysql_num_rows($result);
for($i=0; $i<$numrows; $i++){
$the_arrays=mysql_fetch_array($result);
print("<br /><h2 class="sub">{$the_arrays['title']}</h2>\n");
print("<br /><span style="font-size:8pt;">posted: <i>{$the_arrays['date']}</i>"
."\nID: <i>{$the_arrays['id']}</i></span>\n");
print("<br />{$the_arrays['posttext']}");
}
?>
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

Thanks,

As a noob, I didn't remember where the ORDER BY was supposed to be tagged on. That's a pretty cool snippet. Thanks
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post by jslick »

welcome :)
I did have it
$query="SELECT * FROM news_posts"
."ORDER BY id DESC";
but I got a sytax error because it didn't recognize any space between news_posts and ORDER so I just put it on one line.

I'm sort of a "noob" myself (at SQL). I am getting the hang of it now. I already knew PHP and I finally found a use for SQL so I'm learning it.
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

I did it the other way around. i.e. found a use for MySQL first and needed to learn how to 'talk' to it. :)

Thank God for this forum and others like it. If it weren't for these guys, I would still be at the 'Hello World' stage.
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post by jslick »

steedvlx wrote:I did it the other way around. i.e. found a use for MySQL first and needed to learn how to 'talk' to it. :)

Thank God for this forum and others like it. If it weren't for these guys, I would still be at the 'Hello World' stage.
haha. Why would you learn SQL first?
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

I first used SQL in the days of R:base and Clarion. Therefore, I had a pretty good start in basic MySQL, although there are significant differences from the SQL of ten years ago vs MySQL4 today.

Because of the difference, I get cornered a lot using invalid statements and such.

Since PHP was the natural companion for MySQL, I got to jump in with both feet about a year ago. I mostly insert PHP scripts and my own images (my first love) into other peoples' HTML. My PHP level is pretty low, but fortunately, I don't have to write whole pages using it like the folks who wrote this forum package.
Post Reply