Jump to content

Limit an array


mrt003003

Recommended Posts

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 :)

Link to comment
Share on other sites

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']){
          ...
     }
}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'];
}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.