...

View Full Version : Concatenate a field name with a variable



thepocketgeek
05-05-2009, 09:51 PM
In .ASP you can easily append a variable to a field name from a form like the example below:


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.



$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?

venegal
05-05-2009, 09:55 PM
$i = 1;
$field = $_POST['code'.$i];
echo $field;

Fou-Lu
05-05-2009, 10:01 PM
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.


*shakes fist at venegal*

thepocketgeek
05-05-2009, 10:38 PM
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.



<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:



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


Does that make more sense?

Fou-Lu
05-05-2009, 10:41 PM
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.

thepocketgeek
05-05-2009, 10:51 PM
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.

Fou-Lu
05-05-2009, 11:14 PM
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.


$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?

KenG
01-30-2012, 04:44 AM
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

KenG
01-30-2012, 04:50 AM
I have just found it:
for ($x = 1; $x < 4; $x++) {
echo ${'Code'.$x};
}

BluePanther
01-31-2012, 01:24 AM
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


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:


$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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum