?
Solved

defining variables inside a counter

Posted on 2011-05-12
3
Medium Priority
?
190 Views
Last Modified: 2012-08-14
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?
 
0
Comment
Question by:jmarx75
3 Comments
 
LVL 17

Accepted Solution

by:
nplib earned 1000 total points
ID: 35748653
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
 
LVL 61

Expert Comment

by:HainKurt
ID: 35748667
try

eval("\$candidateid".$i." = \"".$candidatelist['id']."\";");
0
 
LVL 13

Assisted Solution

by:haloexpertsexchange
haloexpertsexchange earned 1000 total points
ID: 35748668
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question