EchoFool Posted July 16, 2008 Share Posted July 16, 2008 Not sure if i understand this correctly but im getting an error to do with my array syntax.... the idea is it creates a list of id's then picks one at random, but i cannot get it to work... any help is much appreciated. Parse error: syntax error, unexpected '[', expecting ')' in C:\xampp\htdocs\name.php on line 427 Im unsure on what i have done wrong. This is what i got: <?php $SELECT = mysql_query("SELECT ID FROM users") Or die(mysql_error()); $a = 0; While($row = mysql_fetch_assoc($Find)){ $ID = $row['ID']; Array ( [$a] => $ID; ) $a = $a + 1; } shuffle($a); $_SESSION['MissionID'] = $a; ?> Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/ Share on other sites More sharing options...
rhodesa Posted July 16, 2008 Share Posted July 16, 2008 what are you trying to accomplish with: Array ( [$a] => $ID; ) ..since it's not valid PHP code Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591799 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 You have done almost everything wrong. This is what I believe you are trying to do: <?php $Find = mysql_query("SELECT ID FROM users") Or die(mysql_error()); $a = array(); While($row = mysql_fetch_assoc($Find)){ $a[] = $row['ID']; } shuffle($a); $_SESSION['MissionID'] = $a; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591801 Share on other sites More sharing options...
EchoFool Posted July 16, 2008 Author Share Posted July 16, 2008 Oh i see thankyou for pointing me in the right direction. With your suggestion Ken, i produce this error: Fatal error: Cannot use [] for reading in C:\xampp\htdocs\missioninclude.php on line 426 Which relates to $a[] = $row['ID'];. Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591806 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 Did you put in <?php $a = array(); ?> Can you post the code you're now using? Ken Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591809 Share on other sites More sharing options...
EchoFool Posted July 16, 2008 Author Share Posted July 16, 2008 Sure here it is: <?php $Find = mysql_query("SELECT ID FROM users") Or die(mysql_error()); If(mysql_num_rows($Find)>0){ $a = array(); While($row = mysql_fetch_assoc($Find)){ $ID = $row['ID']; $a[] = $ID; // this is where the error is } shuffle($a); $_SESSION['MissionID'] = $a; } ?> Thank you for replying Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591817 Share on other sites More sharing options...
EchoFool Posted July 16, 2008 Author Share Posted July 16, 2008 Argh i fixed my error. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591851 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 What was the error? Ken Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591860 Share on other sites More sharing options...
EchoFool Posted July 16, 2008 Author Share Posted July 16, 2008 Changed : $ID = $row['ID']; $a[] = $ID; to $a[] = $row['ID']; Though when i shuffle($a); Then Echo $a; I get "Array" as the response rather than the id. Why could that be? Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591865 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 You can't just echo an array, you need to use something like print_r: <?php echo '<pre>' . print_r($a,true) . '</pre>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591871 Share on other sites More sharing options...
EchoFool Posted July 16, 2008 Author Share Posted July 16, 2008 I see so that produces: Array ( [0] => 25 ) Is there a way to extract the 25 to process with that ID? Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591873 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 $a[0] is how you reference it, or are you asking for something else? Ken Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591879 Share on other sites More sharing options...
wildteen88 Posted July 16, 2008 Share Posted July 16, 2008 Reading your first post, $a should hold a collection of ids correct? and you're want to select an id from the $a array at random? Well change $_SESSION['MissionID'] = $a; to $_SESSION['MissionID'] = array_shift($a); Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591881 Share on other sites More sharing options...
EchoFool Posted July 16, 2008 Author Share Posted July 16, 2008 Oh i see. So array's start at [ 0 ] not [1] that would explain why I couldn't grab the value! Thanks Ken. wildteen88 - is array_Shift similar to my shuffle function? Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-591897 Share on other sites More sharing options...
wildteen88 Posted July 17, 2008 Share Posted July 17, 2008 No, array_shift will remove the first item from the array and return its value. shuffle randomises the order of the items within the array. Actually dump shuffle and array_shift and just use array_rand instead. Quote Link to comment https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/#findComment-592646 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.