allex01 Posted March 5, 2008 Share Posted March 5, 2008 I want to shuffle the array in $results. What's the simplest way to do that. $query = "SELECT product_name FROM products" ; $results = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($results)) { echo = $row['product_name']; } Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 5, 2008 Share Posted March 5, 2008 <?php $query = "SELECT product_name FROM products" ; $results = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($results)) { $results2= $row['product_name']; shuffle($results2); echo $results2; }?> Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted March 5, 2008 Share Posted March 5, 2008 what do you mean shuffle? do you mean order them randomly? if so, try this: $query = "SELECT product_name FROM products ORDER BY RAND()" ; Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 5, 2008 Share Posted March 5, 2008 that works too Quote Link to comment Share on other sites More sharing options...
allex01 Posted March 5, 2008 Author Share Posted March 5, 2008 Thanks for both solutions. Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted March 5, 2008 Share Posted March 5, 2008 you're welcome Quote Link to comment Share on other sites More sharing options...
allex01 Posted March 5, 2008 Author Share Posted March 5, 2008 <?php $query = "SELECT product_name FROM products" ; $results = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($results)) { $results2= $row['product_name']; shuffle($results2); echo $results2; }?> Just a question. If i used this solution. Wouldn't this cause the result to appear more then once. How will it know the result was not already output before. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 5, 2008 Share Posted March 5, 2008 it should not output more than once man it is correct. because $result2 equals row product name. then we shuffle the result2 which randomizes the product_name then you echo result2 which outputs your randomized answer. Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 5, 2008 Share Posted March 5, 2008 Use the ORDER BY RAND() solution. Much more efficient, and IMO the proper way to do it in this case. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 5, 2008 Share Posted March 5, 2008 doesnt matter they both do the same thing one is not more proper than the other 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.