morocco-iceberg Posted May 9, 2010 Share Posted May 9, 2010 I'm trying to get an array that I've populated using a database to shuffle so that I can use it to ouput a random question. Here's my code: <?php mysql_connect("server", "user", "password") or die(mysql_error()); echo "Connected to MySQL<br/>"; mysql_select_db("database") or die(mysql_error()); echo "Connected to Database"; $result = mysql_query('SELECT * FROM table') or exit(mysql_error()); while ($row = mysql_fetch_assoc($result)){ $array[] = $row; } shuffle($array); print_r($array); ?> This doesn't seem to shuffle it at all... Any help is greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/201150-getting-an-array-populated-by-a-database-to-shuffle/ Share on other sites More sharing options...
JAY6390 Posted May 9, 2010 Share Posted May 9, 2010 Why not let MySQL do it for you SELECT * FROM table ORDER BY RAND() Link to comment https://forums.phpfreaks.com/topic/201150-getting-an-array-populated-by-a-database-to-shuffle/#findComment-1055381 Share on other sites More sharing options...
niranjan81 Posted May 9, 2010 Share Posted May 9, 2010 If its a fixed set of questions that re modified rarely, why bother database at all, built one yourself. that will save a round to database. Link to comment https://forums.phpfreaks.com/topic/201150-getting-an-array-populated-by-a-database-to-shuffle/#findComment-1055393 Share on other sites More sharing options...
JAY6390 Posted May 9, 2010 Share Posted May 9, 2010 What exactly does the print_r show? the code you have should work as it is Link to comment https://forums.phpfreaks.com/topic/201150-getting-an-array-populated-by-a-database-to-shuffle/#findComment-1055417 Share on other sites More sharing options...
morocco-iceberg Posted May 10, 2010 Author Share Posted May 10, 2010 I was using the print_r to show the entire array. This is what I get when I print it out... I think the problem happens when I'm tryinto put it into an array, but I can't find any solutions. Array ( [0] => Array ( [Question 1.] => [Question 2.] => [Question 3.] => [Question 4.] => [Question 5.] => ) ) Link to comment https://forums.phpfreaks.com/topic/201150-getting-an-array-populated-by-a-database-to-shuffle/#findComment-1055634 Share on other sites More sharing options...
morocco-iceberg Posted May 10, 2010 Author Share Posted May 10, 2010 Don't worry... my database was all screwed up. Thanks for the help though. Link to comment https://forums.phpfreaks.com/topic/201150-getting-an-array-populated-by-a-database-to-shuffle/#findComment-1055646 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.