PHP consecutive variables

Discussion in 'Web Design & Coding' started by arson_nick, Sep 21, 2003.

  1. arson_nick

    arson_nick OSNN Addict

    Messages:
    126
    Is there a nice way to print out consecutively named variables like Track1, Track2, Track3 ?
     
  2. j79zlr

    j79zlr Glaanies script monkey Political User

    Messages:
    2,725
    Location:
    Chicago
    I didn't try this, but if you used an array it would be easy:

    PHP:
    $variables 10;
    $i 0;
    while (
    $i $variables) {
    echo 
    "$Track[$i]";
    ++
    $i;
     
  3. arson_nick

    arson_nick OSNN Addict

    Messages:
    126
    That's genius, and I wish I'd thought of it. Makes reading the variables in a bit easier too. Thank you!
     
  4. j79zlr

    j79zlr Glaanies script monkey Political User

    Messages:
    2,725
    Location:
    Chicago
    Your welcome, hardly genius, but I'll take it ;)

    Oops, I had a booboo above, I'll edit it, I had ++$x where it should have been ++$i
     
  5. j79zlr

    j79zlr Glaanies script monkey Political User

    Messages:
    2,725
    Location:
    Chicago
    OK, just like any code, I think that there are acouple things that need to be changed;

    PHP:
    $variables 10;
    $i 0;
    while (
    $i <= $variables) {
    echo 
    "$Track[$i]";
    ++
    $i;
    }
    See, now I changed the loop so its 'less than or equal to' so it will print all instead of skipping the last one. Another problem is that the array numbering starts at 0 and not 1. You could always start at
    Track[1]=Track 1 value
    and so forth and change $i=0 to $i=1.
     
  6. arson_nick

    arson_nick OSNN Addict

    Messages:
    126
    I figured I'd just initialize it with
    PHP:
    $Track = array("zero")
     
  7. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    arson_nick just doing

    PHP:
    $Trakc = array();
    Will work fine.

    j79zlr this:

    PHP:
    $variables 10;
    $i 0;
    while (
    $i <= $variables) {
    echo 
    "$Track[$i]";
    $i++;
    }
    Note the $i++;, would in this example you gave, do the same as

    PHP:
    $variables 10;
    $i 0;
    while (
    $i <= $variables) {
    echo 
    "$Track[$i]";
    ++
    $i;
    }
    Even better code would be:

    PHP:
    foreach($Track as $value) {
    echo 
    $value;
    }
    This will let him loop thru the entire array, EVEN when its bigger than the 10 variables you had set it to :).

    Check out http://php.net/foreach for more info.
     
  8. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Now, if you did not have it in an array, then this would help:

    PHP:
    $x 0;
    while (
    1) {
    $var "Track" $x;
    echo ${
    $var};
    $x++;
    $var "Track" $x;
    if (${
    $var} == "") {
    break;
    }
    }
    This will loop thru variables like this:

    $Track1
    $Track2
    $Track3
    $Track4
    etc ...
     
  9. arson_nick

    arson_nick OSNN Addict

    Messages:
    126
    That's exactly what I was looking for. The array was giving me a lot of problems as far as passing it through a form. Thanks again.