Jump to content

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
https://forums.phpfreaks.com/topic/237631-limit-an-array/
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
https://forums.phpfreaks.com/topic/237631-limit-an-array/#findComment-1221064
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
https://forums.phpfreaks.com/topic/237631-limit-an-array/#findComment-1221090
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
https://forums.phpfreaks.com/topic/237631-limit-an-array/#findComment-1221177
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.