Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-31-2009, 02:33 PM   PM User | #1
Blatch
New Coder

 
Join Date: Mar 2003
Location: USA
Posts: 99
Thanks: 24
Thanked 0 Times in 0 Posts
Blatch is an unknown quantity at this point
Display in random order

I have a db of items with `id`s. And I'm currently displaying them with a loop in DESC order (5, 4, 3, 2, 1).

PHP Code:
$query mysql_query("SELECT * FROM `table` ORDER BY `id` DESC");
while(
$row mysql_fetch_array($query)) {
    echo 
'This is item number' $row["id"] . '<br />';

How can I modify this so it will display like this instead:

Code:
This is item number 3
This is item number 1
This is item number 2
This is item number 4
This is item number 5
Randomly. Thanks!
Blatch is offline   Reply With Quote
Old 08-31-2009, 02:40 PM   PM User | #2
SKDevelopment
Regular Coder

 
Join Date: Mar 2006
Posts: 238
Thanks: 3
Thanked 37 Times in 37 Posts
SKDevelopment has a little shameless behaviour in the past
PHP Code:
$query mysql_query("SELECT * FROM `table` ORDER BY RAND()");
while(
$row mysql_fetch_array($query)) {
    echo 
'This is item number' $row["id"] . '<br />';

__________________
PHP Programmer
SKDevelopment is offline   Reply With Quote
Users who have thanked SKDevelopment for this post:
Blatch (08-31-2009)
Old 08-31-2009, 02:40 PM   PM User | #3
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
PHP Code:
$ids = array();

$query mysql_query("SELECT * FROM `table` ORDER BY `id` DESC");
while(
$row mysql_fetch_array($query))
    
$ids[] = $row['id'];

shuffle($ids);

foreach ( 
$ids as $id )
    echo 
'This is item number' $id '<br />';

Mwnciau is offline   Reply With Quote
Users who have thanked Mwnciau for this post:
Blatch (08-31-2009)
Old 08-31-2009, 02:53 PM   PM User | #4
Blatch
New Coder

 
Join Date: Mar 2003
Location: USA
Posts: 99
Thanks: 24
Thanked 0 Times in 0 Posts
Blatch is an unknown quantity at this point
Thank you both!
Blatch is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:32 PM.


Advertisement
Log in to turn off these ads.