Jax2 Posted April 11, 2012 Share Posted April 11, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260744-having-a-hell-of-a-time-with-arrays-need-some-help-please/ Share on other sites More sharing options...
Muddy_Funster Posted April 11, 2012 Share Posted April 11, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/260744-having-a-hell-of-a-time-with-arrays-need-some-help-please/#findComment-1336383 Share on other sites More sharing options...
Jax2 Posted April 11, 2012 Author Share Posted April 11, 2012 Muddy, changed it around just a little and it works perfectly. I even learned what I was doing wrong, so thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/260744-having-a-hell-of-a-time-with-arrays-need-some-help-please/#findComment-1336441 Share on other sites More sharing options...
Muddy_Funster Posted April 11, 2012 Share Posted April 11, 2012 You're welcome Quote Link to comment https://forums.phpfreaks.com/topic/260744-having-a-hell-of-a-time-with-arrays-need-some-help-please/#findComment-1336444 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.