Jump to content

variable variables


Recommended Posts

My PHP-generated form file will generate any number of text fields called "recipient"+an integer, so "recipient1", "recipient2", etc The PHP file handling the form submission will not know how many fields there are, so I need to get something like the following logic:

 

$i=1;

$max=how many fields there are (it will know this from a mysqli_num_rows variable being passed)

while ($i <= $max) {

$recipient+$i = $_POST[recipient+$i]

}

 

In other words, as long as $i is less than or equal to the maximum number, generate a variable named "recipient" concatenated with $i and assign it the value of $_POST['recipient concatenated with $i'].

 

I can't wrap my little brain around this. Help?

Link to post
Share on other sites

I need to write PHP code to handle a form. The form may have zero or more fields that are named "recipient1", "recipient2", "recipient3", etc. If there are two fields, I need to capture those fields. If there are six, I need to capture all six. Maybe there are none.

 

I could write

 

if (isset($_POST['recipient1'])) {

$thisvariable1=$_POST['recipient1'];

}

 

if (isset($_POST['recipient2'])) {

$thisvariable2=$_POST['recipient2'];

}

 

if . . . and so on. Maybe I could do 10 of these, but what if the form has 11 fields (recipient1 through recipient11)? I can't just write a hundred "if" statements. Surely there's some way to write a code that allows me to replace the "1", "2", and so on with some variable.

 

The form itself will pass along how many fields there are. How can I use that number to automatically generate this kind of code that assigns the $_POST data to variables?

 

Does that make more sense? How can I do this? That's my question.

Link to post
Share on other sites

To be more concrete, this code

 

echo $_POST['recipient1'];
echo '<br/>';
echo $_POST['recipient2'];
echo '<br/>';
echo $_POST['recipient3'];
echo '<br/>';
echo $_POST['recipient4'];
echo '<br/>';
$i=1;
while ($i<=$how_many) {
${"variable_$i"}=$_POST{"['recipient$i']"};
echo 'variable '.$i.' is ';
echo ${"variable_$i"};
echo '<br/>';
$i=$i+1;
}
yields this:
612113variable 1 is variable 2 is variable 3 is variable 4 is
Why can't I get the "while" loop to give me the same results as the statements before the "while" loop? (The variable $how_many equals 4 in this example.)
Edited by mlitch
Link to post
Share on other sites

The problem is with how to use curly braces with the $_POST variable, because this code:

 

$i=1;
while ($i<=$how_many) {
${"variable_$i"}=1000+$i;
echo 'variable '.$i.' is ';
echo ${"variable_$i"};
echo '<br/>';
$i=$i+1;
}
yields (as I expect):
variable 1 is 1001variable 2 is 1002variable 3 is 1003variable 4 is 1004
Link to post
Share on other sites

Why don't you use an array? That's what they're for.

 

In the $_POST array you're supposed to wrap string indices in quotation marks. In PHP, the concatenation operator is ., not +.

$recipient = array();while ($i <= $max) {  $recipient[$i] = $_POST['recipient' . $i]}
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...