Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    Dymanic Variables In PHP, possible? Need Help.

    Hi there,

    Having some issues with a variable.
    I have a query running that returns 10 results of data, in a while loop.
    During the while is open it does something like :
    $data = Result Data
    For each row, and then after prints..
    PRINT $data;
    That works however I'm now trying to give dymanic variables, so I can count the rows. So id have a count in the row return ..
    $count = $count +1;
    $data = Result Data
    And then I would like to use that count to name each row of data, so instead of
    $count = $count +1;
    $data = Result Data
    I then get ..
    $count = $count +1;
    $data.$count = Result Data
    // This should be called using PRINT $data1;
    That's what I'm after, however stuggilin ammending the count to the end of the variable name, tried a few ways but not got anywhere..

    Is this not possible? Or is there special syntax to assign another variable to the variable name?

    Or should I be using another while or foreach statement?
    Any information be apperciated.

    Cheers all
    Sir P

  • #2
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    cant understand what your trying to do. If your trying to give each one a id then why not try:

    PHP Code:
    $num_rows mysql_num_rows($result);
    $i=0;
    while (
    $i $num_rows) {
    $data Result Data
    echo "$i:$data";
    $i++


  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    hi thanks for your reply...

    I need to be able to call the data rows individually later on in the script though.. so for example, i have ran my query and had 10 results returned.. later on in the script I want to call the 3rd row of data... doing something like

    print $data3

    or something..
    am I explaining badly? could be the case ... sorry

  • #4
    New Coder
    Join Date
    Aug 2006
    Location
    Netherlands
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have a look at the extensive explanation of variable variables at the php.net site.
    Link: http://nl3.php.net/manual/en/languag...s.variable.php

    Another matter: why don't you use an array to stroe your result, such as:
    $data= array();
    $count = $count +1;
    $data[$count] = Result Data;
    That way, if you wanted data3, just address it as
    echo $data[3];
    Ronald
    Last edited by ronverdonk; 08-24-2006 at 01:34 PM.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks for the link.

    However none of the information there can be applied to this situation at the best of my knowledge, I just read though it and didn't see anything about changing variable names and what not ...

    That being said, your array solution will work perfectly, thanks very much for your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •