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 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post

    Incrementing a variable's name

    I've created a table with six quantity/price pairs of values (among other items). Is there a better way to display these pairs other than creating a line for each pair?

    So instead of ...
    Code:
    echo $row->qty1 . " " . $row->price1;
    echo $row->qty2 . " " . $row->price2;
    etc...
    That I could loop through the variable names?

    Code:
    for $x = 1 to 6
    $vname1 = "qty" . $x;
    $vname2 = "price" . $x;
    echo $row->(vname1) . " " . $row->($vname2);
    next
    Thanks
    Of all the things I've lost in my life time, my mind is the one I miss the most!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    1st Normal Form is broken here. Any time you see column names ending in "1", "2", etc, you know you need to break that data into a new table and use a foreign key to relate it to the main table.

  • #3
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post
    I knew I would get taken to task over that one. I know that I should but in this case I am taking a poor form short cut intentionally.

    But... is my original question possible in PHP?
    Of all the things I've lost in my life time, my mind is the one I miss the most!

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    It's possible, using variable variables. But you're looping over them already, so I don't see why you would need/want to.

  • #5
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    personally I tent to to use while loops rather than for each as loops for some reason seated in other languages.

    PHP Code:
    $foo 0;
    while( 
    $foo 6){
    //echo[$foo] etc....
    $foo++; 


  • #6
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    You nearly had the code right there

    PHP Code:
    for($x 1$x <= 6$x++) {
      
    $vname1 'qty'.$x;
      
    $vname2 'price'.$x;
      echo 
    $row->$vname1.' '.$row->$vname2;

    I would've thought you've gotten $row from a db call though?

    PHP Code:
    foreach(mysql_fetch_object($result_id) as $row) {
      echo 
    $row->qty.' '.$row->price;

    Last edited by Ahri; 11-23-2007 at 03:00 PM. Reason: Switched code -> php tags
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini


  •  

    Posting Permissions

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