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 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    73
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Concatenate a field name with a variable

    In .ASP you can easily append a variable to a field name from a form like the example below:
    Code:
    i = 1
    field = Request.Form("code" & i)
    So the field "code" is changed to code1. Is there a way to do this in PHP? I am in the middle of converting an ASP site to PHP and I'm not sure how to fix this one without completely rewriting the loop where this is contained. All I need to do loop through 10 field names that are the same and add a number to the end of the field name through each iteration.

    PHP Code:
    $i 1;
    $field  $_POST['code'].$i;
    echo 
    $field
    The above code appends the one to the VALUE of code as opposed to the field name. Is there any way to affect the change to the field name instead of the value of the $_POST?

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    PHP Code:
    $i 1;
    $field  $_POST['code'.$i];
    echo 
    $field

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    If the html input name is say: 'code1', than use $_POST['code' . $i];
    If the html input name is say: 'code[1]' (recommended), than use a multi-dimensional array: $_POST['code'][$i].
    The multidimensional route is the one I'd recommend. It lets you count, iterate and do some nifty things that you can't do as easily with the concat method.

    Edit:
    *shakes fist at venegal*
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    thepocketgeek (05-05-2009)

  • #4
    New Coder
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    73
    Thanks
    11
    Thanked 0 Times in 0 Posts
    I don't think I explained the problem well enough. If I use the concatenate method, I end up appending the value of $i to whatever the user inputs in the field. What I need to do is to add the value of $i to the field NAME.

    PHP Code:
    <input name="Code1" />
    <input name="Code2" />

    <?php 
    $i 
    1;
    if(
    strlen($_POST['Code'].$i)){
     echo 
    "The field name should be Code1";
    }
    ?>
    The above code changes the VALUE of $_POST['Code']. I need it to read the statement as if was written like this:

    PHP Code:
    if(strlen($_POST['Code1'])){
     echo 
    "The field name should be Code1";

    Does that make more sense?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep, and thats what this solution will provide you with: $_POST['Code' . $i];
    PHP will see this as $_POST['Code1'], $_POST['Code2']... up wo whatever you're $i is if you're using a loop.

    The array method is a lot easier, since you could iterate it with a for or foreach loop.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    thepocketgeek (05-05-2009)

  • #6
    New Coder
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    73
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thanks. That worked like a charm. If I was building the app from scratch I would go with the multi-dimensional array. But since I am converting, I'll go with this for now.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by thepocketgeek View Post
    Thanks. That worked like a charm. If I was building the app from scratch I would go with the multi-dimensional array. But since I am converting, I'll go with this for now.
    NP.
    Keep the array idea in mind, it will simplify you're work in the future.
    And, I'll say that you shouldn't use strlen for this one (at least for what you have). Its tricky, but what you'll need first is to check if the variable exists (to prevent notices), then check to see if its empty.
    PHP Code:
    $i 1;
    $code = isset($_POST['Code' $i]) ? trim($_POST['Code' $i]) : '';
    if (!empty(
    $code))
    {
        
    // Do stuff.

    Though strlen will work, I'd go with !empty. Unless you're looking for a specific character length, than strlen is the way to go (you don't need to check both empty and length). However, always check for isset(). Language constructs will allow you to provide them with invalid data and not trigger an error, while functions require valid data (isset is a construct, empty is a function). So, if $_POST['Code1'] does not exist, using isset() will return false with no warning, while empty will return true with a warning.
    Clear as mud?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Another way to do this if you have variables Code1, Code2, Code3 and you want to iterate through them then you can do:
    for ($x = 1; $x < 4; $x++) {
    $temp = 'Code'.$x;
    echo $$temp;
    }

    I tried various other ways like:
    echo $.'Code'.$x;
    but I couldn't find another way other than having to use the $temp variable

  • #9
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have just found it:
    for ($x = 1; $x < 4; $x++) {
    echo ${'Code'.$x};
    }

  • #10
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by KenG View Post
    Another way to do this if you have variables Code1, Code2, Code3 and you want to iterate through them then you can do:
    for ($x = 1; $x < 4; $x++) {
    $temp = 'Code'.$x;
    echo $$temp;
    }

    I tried various other ways like:
    echo $.'Code'.$x;
    but I couldn't find another way other than having to use the $temp variable
    Quote Originally Posted by KenG View Post
    I have just found it:
    for ($x = 1; $x < 4; $x++) {
    echo ${'Code'.$x};
    }
    Variable variables are bad practise and should always be avoided (they always can be avoided). Doing this would be much better:
    PHP Code:
    $i=1;
    // Use a while - it will iterate every time array_key_exists() returns true
    while(array_key_exists('Code'.$i,$_POST)){
        
    // Do stuff with $_POST['Code'.$i]

        // Iterate $i to check for more Code+numbers
        
    $i++;

    Also, why resurrect such an old post?
    Last edited by BluePanther; 01-31-2012 at 12:26 AM.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

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