...

View Full Version : Display in random order



Blatch
08-31-2009, 02:33 PM
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).


$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:


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!

SKDevelopment
08-31-2009, 02:40 PM
$query = mysql_query("SELECT * FROM `table` ORDER BY RAND()");
while($row = mysql_fetch_array($query)) {
echo 'This is item number' . $row["id"] . '<br />';
}

Mwnciau
08-31-2009, 02:40 PM
$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 />';
}

Blatch
08-31-2009, 02:53 PM
Thank you both!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum