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 Quote Link to comment 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; ?> Quote Link to comment 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"; Quote Link to comment 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.