mrt003003 Posted May 27, 2011 Share Posted May 27, 2011 Hi there, im really stuck here. I have a table of records that are selected from my db and put into an array and randomly chooses them, outputting the results . <?php $count = 0; while ($row = mysql_fetch_assoc($Ships4)){ $array[$count]['ShipID'] = $row['ShipID']; $array[$count]['ShipName'] = $row['ShipName']; $array[$count]['Dclass'] = $row['Dclass']; $count++; $total4 = count($array); $random4 = rand(0,$total4 - 1); $random_ship4 = $array[$random4]; $ShipID4 = $random_ship4['ShipID']; echo $ShipID4;} This all works fine, however I want to limit the number of random outputs from a number in a record in a different table. So i added: <?php if ($count < $row_dclasscorella['Dclass4_totla']){ ?> the number from the field is 2. The trouble is that it doesnt take any notice and outputs the total number a random number for each record. It seems its not limiting it. How can i get this to work please??? $count = 0; if ($count < $row_dclasscorella['Dclass4_totla']){ while ($row = mysql_fetch_assoc($Ships4)){ $array[$count]['ShipID'] = $row['ShipID']; $array[$count]['ShipName'] = $row['ShipName']; $array[$count]['Dclass'] = $row['Dclass']; $count++; $total4 = count($array); $random4 = rand(0,$total4 - 1); $random_ship4 = $array[$random4]; $ShipID4 = $random_ship4['ShipID']; echo $ShipID4;}}?> Thank You Quote Link to comment https://forums.phpfreaks.com/topic/237631-limit-an-array/ Share on other sites More sharing options...
cyberRobot Posted May 27, 2011 Share Posted May 27, 2011 Based on the code provided, the if test is only going to execute once. If you want it to execute every time the while loops, you'll need to modify the code: <?php $count = 0; while ($row = mysql_fetch_assoc($Ships4)){ if ($count < $row_dclasscorella['Dclass4_totla']){ ... } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237631-limit-an-array/#findComment-1221064 Share on other sites More sharing options...
mrt003003 Posted May 27, 2011 Author Share Posted May 27, 2011 Yes thats it, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/237631-limit-an-array/#findComment-1221089 Share on other sites More sharing options...
mrt003003 Posted May 27, 2011 Author Share Posted May 27, 2011 Ive just spotted something that truely doesnt make sense, now my outputted random numbers are random but are only ever either 2 or 3??? There are 3 records that could be randomised (2,3,4) but only ever 2 and 3 are outputted. Why are 4's not being used?? Infact the outputed numbers are only ever: 2,3 or 2,2. The first number is always a 2?? Its as if the limit of 2 is stopping the array from using all the avaliable ShipIDs and only allowing the first 2. Oh man im confussed Thanks Quote Link to comment https://forums.phpfreaks.com/topic/237631-limit-an-array/#findComment-1221090 Share on other sites More sharing options...
The Little Guy Posted May 27, 2011 Share Posted May 27, 2011 If I understand correctly, I would just use array_rand while ($row = mysql_fetch_assoc($Ships4)){ $array[$count]['ShipID'] = $row['ShipID']; $array[$count]['ShipName'] = $row['ShipName']; $array[$count]['Dclass'] = $row['Dclass']; $count++; } // 3 random items from the array $keys = array_rand($array, 3); foreach($keys as $key){ echo $array[$key]['ShipID']; echo $array[$key]['ShipName']; echo $array[$key]['Dclass']; } Quote Link to comment https://forums.phpfreaks.com/topic/237631-limit-an-array/#findComment-1221177 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.