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 11 of 11
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Trying to understand variable variables

    Hi,

    I want to create a variable name based on a counter which increases.

    After reading the brief note in the Manual, I still can't grasp it !

    I am trying to make $N_earn1 = $earn and $N_earn2 = $earn etc. etc.

    This is my code:
    PHP Code:
    $sql "SELECT earn, comm,  FROM main WHERE id = '$Db_prod' ORDER BY mday_no DESC ";
    $result  mysql_query($sql)    or die("could not execute SELECT cb_main"mysql_error());  
    $num mysql_num_rows($result);
    if (
    $num == ) {
       
    $earn1 =0;
       }
    else {
           
    $ctr 1;
           while( 
    $row mysql_fetch_assoc($result) ) {  
                 
    extract($row);
                 
    $var  'N_earn'.$ctr;
                 $
    $var $earn;

                 
    $ctr++;
             }

    Is this the correct way to use it ?

    When ctr = 1 is:

    $$var = $earn; the equivelent of $N_earn1 = $earn; ?



    .
    Last edited by jeddi; 02-08-2010 at 05:21 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    That looks correct to me.
    Put an echo in to test it ...

    while( $row = mysql_fetch_assoc($result) ) {
    extract($row);
    $var = 'N_earn'.$ctr;
    $$var = $earn;

    echo $N_earn1;
    exit;


    $ctr++;
    }

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Edited: One day I really will learn to read the questions instead of just scanning the code.
    Last edited by MattF; 02-08-2010 at 10:13 PM.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    These lines:
    PHP Code:
    $var  'N_earn'.$ctr;
    $
    $var $earn
    Can also be written like so:
    PHP Code:
    ${ 'N_earn' $ctr } = $earn
    Saving you having to assign a dummy variable.

    I'd also recommend looking into using an array as opposed to variable variables. It'll be easier to track what you've assigned:
    PHP Code:
    $N_earn = array();

    /* ... */

    $ctr 1;
    while( 
    $row mysql_fetch_assoc$result ) )
    {
        
    $N_earn$ctr++ ] = $row['earn'];

    Last edited by kbluhm; 02-09-2010 at 09:28 AM.

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for your replies.

    Actually I am trying to create an array of data with this code
    any way, and I have the feeling that the $row array may already be what I want !

    This is what I want to replace:

    $earn = array(130,310,410,310,340,400);
    $comm = array(310,652,410,310,76,40,361,256);


    So instead of the that static data, I want to pick up the data
    "earn" and "comm" from the table.

    Maybe I do not need to extract the $row ?
    nor use the variable variables ?

    Something like this:
    PHP Code:
    $earn = array();
    $comm = array();
    $sql "SELECT earn, comm,  FROM main WHERE id = '$Db_prod' ORDER BY mday_no DESC ";
    $result  mysql_query($sql)    or die("could not execute SELECT cb_main"mysql_error());  
    $num mysql_num_rows($result);
    if (
    $num == ) {
       
    $earn[] = 0;
       
    $comm[] = 0;
       }
    else {
           while( 
    $row mysql_fetch_assoc($result) ) {  
                
    $earn[] = $row['earn'];
                 
    $comm[] =  $row['comm'];   
                }
          } 

    If I can do it this way, then I don't need the counter
    or those variable variables ?

    Is this the better way to go ?


    I have tried this and I am getting a problem with it:

    When I display the resulting array with this:
    print_r($earn);

    I get:

    Array ( [0] => Array ( ) [1] => 34.01 [2] => 33.36 [3] => 33.36 [4] => 82.25 [5] => 82.25 )

    So I seem to have created a double array somehow

    And hence max(earn); just returns "Array" rather than the
    maximum value.

    What am I doing wrong ?



    .
    Last edited by jeddi; 02-09-2010 at 12:06 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by jeddi View Post
    Actually I am trying to create an array of data with this code any way, and I have the feeling that the $row array may already be what I want !
    I doubt that, $row holds only the values from 1 DB row (that is, 4 members).

    Quote Originally Posted by jeddi View Post
    This is what I want to replace:

    $earn = array(130,310,410,310,340,400);
    $comm = array(310,652,410,310,76,40,361,256);


    So instead of the that static data, I want to pick up the data
    "earn" and "comm" from the table.

    Maybe I do not need to extract the $row ?
    unfortunately, you have (with this code) no other way of getting the data, since $result (which holds all data) is not an array.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi Dormilich,

    Maybe you did not see the while loop ???

    I walk through the $row[] with it.

    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by jeddi View Post
    Maybe you did not see the while loop ???
    I’ve seen that, but I thought you meant something different.

    Quote Originally Posted by jeddi View Post
    I walk through the $row[] with it.
    nope. you walk through $result with that.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    nope. you walk through $result with that.
    yer, OK

    But the point is ... do you see whats wrong with this?

    To me it looks like it should work - why am I getting this "double array"


    IT OK NOW, I found my the error - it works OK now

    Thanks



    .
    Last edited by jeddi; 02-09-2010 at 04:06 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    You're sure you've posted all of the relevant code from that script? I'll personally be damned if I can tell why it's multi-dimensional from looking at that code above.

  • #11
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi Matt,

    Just to ease your mind ...

    I better confess, I had made a typo because I didn't
    copy and paste the code like I usually do.

    The top two array statements were:
    $earn = array[];
    $comm = array[];

    square brackets instead of rounded.

    so that was what was causing me grief !

    But I had some other problems which you guys helped sort out,
    so it wasn't wasted effort

    and thanks again.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

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