defining variables inside a counter

Posted on 2011-05-12
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))
$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

$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?
Question by:jmarx75
    LVL 17

    Accepted Solution

    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))
    $candidateid[] = $candidatelist['id'];

    Open in new window

    LVL 51

    Expert Comment


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

    Assisted Solution

    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))
    $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'].

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    Where is this Code Igniter page going? 4 22
    Bing Maps 1 23
    Total days in a month 12 27
    CSRF session variables 23 10
    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now