...

View Full Version : random population of select menu



CallumJohnson
08-10-2010, 01:54 PM
Hey,
I have this code:


<?php
echo '<select name="user_banks" class="grey_tbox">';
$sql = ("SELECT * FROM ".TBL_USERPROPBANKS."");
$query = $database->query($sql);
while($bank = mysql_fetch_array($query)){
echo '<option value="'.$bank['prop_id'].'">'.$bank['prop_name'].' ('.$bank['prop_t_rate'].'%)</option>';
}
echo '</select>';
?>

It gets data from my MySQL table and populates the select menu.
However, I want it to populate the position of each row in the select menu randomly.
e.g. the top select row isn't always value =1


I'm a bit stumped on how to actually achieve this. Any ideas?

regards, Callum.

tfburges
08-10-2010, 02:49 PM
Maybe adding ORDER BY RAND to your SQL string will do what you want. Give that a shot.

CallumJohnson
08-10-2010, 03:05 PM
Maybe adding ORDER BY RAND to your SQL string will do what you want. Give that a shot.


$sql = ("SELECT * FROM ".TBL_USERPROPBANKS." ORDER BY RAND()");

Thanks, this works fine but, i've heard its really quite server usage intensive: http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/

So would you suggest i use one of those workarounds?
I'll probably only have 5 rows in the table though, would a LIMIT 5 make it faster?

tfburges
08-11-2010, 09:00 AM
I'm really not sure... I haven't used SQL in years and I prefer not to...

Try these PHP functions:
http://www.php.net/manual/en/function.shuffle.php
http://php.net/manual/en/function.array-rand.php

:)

CallumJohnson
08-11-2010, 09:36 AM
thank you!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum