Jump to content

Recommended Posts

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;
?>

 

Link to comment
https://forums.phpfreaks.com/topic/115086-solved-help-with-array-construction/
Share on other sites

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

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

 

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

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.