tmyonline Posted November 9, 2007 Share Posted November 9, 2007 Hi guys: Here's my problem: $expSQL = "SELECT experiences_key FROM experiences_teachers_jn WHERE schools_key = $s"; $expResult = mysql_db_query($db,$expSQL,$cid); $expRow = @mysql_fetch_assoc($expResult); Because there are many "experiences_key" associated with a given schools_key (many-to-one relationship), after fetching the $expResult, the $expRow is a column of numbers. I would like to randomly select a number from this list (column). However, because these numbers do not form a continuous sequence, if I specify the lower bound and upper bound, there will be chances that the number selected is not on the list. For example, $expRow could be {1, 5, 12, 21,..., 783}. I was thinking of storing these values in an array and go from there but is there a better way to do this ? Thanks. Tommy Link to comment https://forums.phpfreaks.com/topic/76640-solved-how-to-randomly-select-a-number-from-a-discontinuous-set-of-numbers/ Share on other sites More sharing options...
The Little Guy Posted November 9, 2007 Share Posted November 9, 2007 Here is how I would do it: <?php $array = mysql_fetch_array($expSQL); $num = array_rand($array); echo $num; ?> Link to comment https://forums.phpfreaks.com/topic/76640-solved-how-to-randomly-select-a-number-from-a-discontinuous-set-of-numbers/#findComment-388049 Share on other sites More sharing options...
GingerRobot Posted November 9, 2007 Share Posted November 9, 2007 I'd just add an order by RAND() clause on the end of your SQL statment: $expSQL = "SELECT experiences_key FROM experiences_teachers_jn WHERE schools_key = $s ORDER BY RAND() LIMIT 1"; Link to comment https://forums.phpfreaks.com/topic/76640-solved-how-to-randomly-select-a-number-from-a-discontinuous-set-of-numbers/#findComment-388053 Share on other sites More sharing options...
tmyonline Posted November 12, 2007 Author Share Posted November 12, 2007 Thank you both "The Little Guy" and "GingerRobot". You guys are great helps! Link to comment https://forums.phpfreaks.com/topic/76640-solved-how-to-randomly-select-a-number-from-a-discontinuous-set-of-numbers/#findComment-389666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.