Page 1 of 1

[SOLVED] Reversing results.

Posted: Mon Aug 02, 2004 9:25 pm
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]

Posted: Mon Aug 02, 2004 11:13 pm
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;

Posted: Tue Aug 03, 2004 9:06 am
by jslick
Thanks kettle_drum! I used the ORDER BY to get it working.

Posted: Tue Aug 03, 2004 10:11 am
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

Posted: Tue Aug 03, 2004 10:44 am
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']}");
}
?>

Posted: Tue Aug 03, 2004 11:55 am
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

Posted: Tue Aug 03, 2004 12:59 pm
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.

Posted: Tue Aug 03, 2004 8:53 pm
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.

Posted: Tue Aug 03, 2004 9:04 pm
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?

Posted: Tue Aug 03, 2004 11:30 pm
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.