defining variables inside a counter

I am trying to define variables inside a counter for later use. Check this out:
 $i = 0;
while($candidatelist = mysql_fetch_array($candidates))
{
$i++;
$candidateid = $candidatelist['id'];
} 

Open in new window

$candidates comes from a database. The $candidatelist array returns 2 results.
What i want to do is add the counter variable to the $candidateid variable so I can separate them out for later use.
Like I would like $candidateid1, $candidateid2 etc for use in an ajax function. For the life of me I can't get it to work.
I tried things like.
$candidateid.$i =  $candidateid.$i;

Open in new window

and
$candidateid + $i;

Open in new window

But those doesn't seem to work. This seems like a simple thing to do, but i just can't get it to work.
Can anyone help?
 
jmarx75Asked:
Who is Participating?
 
nplibConnect With a Mentor Commented:
create an array, then loop through or convert to sring with implode() or do what ever you want with it.
$i = 0;
$candidateid = array();
while($candidatelist = mysql_fetch_array($candidates))
{
$i++;
$candidateid[] = $candidatelist['id'];
}

Open in new window

0
 
HainKurtSr. System AnalystCommented:
try

eval("\$candidateid".$i." = \"".$candidatelist['id']."\";");
0
 
haloexpertsexchangeConnect With a Mentor Commented:
I don't think that is possible like that.
you are not really concatenating that actual variables with the above code, you are concatenating the values of the variables or adding the values of the variables, not the actual variable names.
Try this.
 $i = 0;
while($candidatelist = mysql_fetch_array($candidates))
{
$i++;
$candidateid[] = $candidatelist['id'];
} 

Open in new window

You will not lose any information due to overwriting the ids are placed inside an array and then later on you can access them like this $candidateid['2'].
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.