Jump to content

Having a hell of a time with arrays - need some help please.


Jax2

Recommended Posts

Ok, I will explain first what I am trying to do, and show you how I am going about it, which isn't working :)

 

I have a table set up. It has missing ID numbers, such as, 1 2 3 6 7 8 11 12 ..etc.

 

I am trying to pull a random ID number from all valid ID numbers. (Meaning, I don't want to get an ID of a record that is no longer there, so skip all the missing ID's)

 

This is for a cron job to change which record is being shown each day, randomly. I figured, logically, the best way to do this would be to pull all the valid ID's into an array, and then use array_rand($id_array,1); to pull one random ID number out of the array.

 

So, I tried using this code:

//Set variable as an array
$id_array = array();

//Populate array with valid ID's
$sql="SELECT ID FROM riddles";
$result=mysql_query($sql,$db) or die(mysql_error());
while($row = mysql_fetch_array($result,$db))
{
    $id_array[] = $row['ID'];
};
print_r(array($id_array));

$new_rotd=(array_rand($id_array[0],1));

 

I am running into 2 problems with this code. First, when I print_r the array, I get the following:

 

Array ( [0] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => ) )

 

The numbers are not correct. It is the correct number of total records, but it is not putting the correct ID number in there.

 

2) When I try and pull a random number out of the array, I get an error saying the First argument has to be an array. It IS an array?!

 

I need to get this working, so any help would be much appreciated. To summarize I need to:

  • Pull all valid ID #'s out of the table and put them in an array.
  • Pull one random number out of that array and set it as variable: $new_rotd

 

 

Link to comment
Share on other sites

ok, try this little change:

//Populate array with valid ID's
$sql="SELECT ID FROM riddles";
$result=mysql_query($sql,$db) or die(mysql_error());
while($row = mysql_fetch_assoc($result,$db))
{
    array_push($id_array,  $row['ID']);
};
$rand_key = array_rand($id_array, 1);
$rand_value= $id_array[$rand_key];
echo $rand_value;

Link to comment
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.