0

I have a form that has radio inputs that have a dynamically generated name like below:

<input type="number" name="<?php echo date("Y")-4 ?>brand%gross" placeholder="Gross Sales % for <?php echo date("Y")-4 ?>">
<input type="number" name="<?php echo date("Y")-3 ?>brand%gross" placeholder="Gross Sales % for <?php echo date("Y")-3 ?>">
<input type="number" name="<?php echo date("Y")-2 ?>brand%gross" placeholder="Gross Sales % for <?php echo date("Y")-2 ?>">
<input type="number" name="<?php echo date("Y")-1 ?>brand%gross" placeholder="Gross Sales % for <?php echo date("Y")-1 ?>">

Then the form is processed and creates a dynamic table with all the values the user inputs that is then emailed to me. My question is how do I get dynamically generated radios to output to the table that is sent? I know the below isn't correct but just to give you a better look at what I'm trying to do:

if( isset($_POST) ){
    ${ echo date("Y")-4 } = $_POST['{ echo date("Y")-4 }];

Any help is greatly appreciated!

1
  • dynamic table? Do you mean rows created based on the radios selected? Commented Jun 7, 2013 at 19:29

3 Answers 3

2

If you want to set a varible content inside $_POST, well... Use a variable.

$foo = date("Y")-4;
$_POST['str'] = $foo;

So... the thing is , you want to use a variable variable name that might go like this:

$foo = date("Y")-4;
$$foo = $_POST[$foo];

Got it?

EDIT 1:

So that the variable won't start with a number:

$bar = "dt_";
$foo = date("Y")-4;
${$bar.$foo} = $_POST[$foo];
Sign up to request clarification or add additional context in comments.

8 Comments

Um, I think you've got a typo in $$foo. ;-)
don't think so... It is a double "$", thats why its variable variable, cause its name is variable.
Hm... interesting. I didn't know that. One learns something new every day.
You could use ${$foo} too , I really don't know the difference, but people seem to prefer it.
Thanks for the additional info. I'll look into that. I remember seeing something related to a similar syntax and there is a purpose, but can't remember off the top of my head right now. Cheers
|
1

By have a string (the output of date()) from which you subtract and integer, you end up casting the result as an integer, not a string. You are probably better off making the string correctly to begin with and getting rid of the integer arithmetic issue.

I would suggest working with DateTime objects. Here's how you might output you input fields.

<?php
$current_date = new DateTime();
while($i = 1; $i <= 4; $i++) {
    $date_int = new DateInterval('P' . (string)$i . 'Y');
    $temp_date = $current_date->sub($date_int);
    $date_string = $temp_date->format('Y');
?>
    <input type="number" name="<?php echo $date_string; ?>brand%gross" placeholder="Gross Sales % for <?php echo $date_string; ?>" /> 
<?php
} // end while
?>

When processing the $_POST you can do similar:

<?php
$current_date = new DateTime();
$year_array = array();
while($i = 1; $i <= 4; $i++) {
    $date_int = new DateInterval('P' . (string)$i . 'Y');
    $temp_date = $current_date->sub($date_int);
    $date_string = $temp_date->format('Y');
    if (!empty($_POST[$date_string. 'brand%gross'])) {
        $year_array[$date_string] = $_POST[$date_string . 'brand%gross'];
    }
} // end while    
?>

Note that I use an array to store your data indexed by year string , as you can't have a variable name that starts with a number (i.e. $2013nrand%gross is not valid).

I would also STRONGLY suggest using array access notation in your inputs to simply things.

If you made each input like this:

<input type="number" name="year_brand_gross[<?php echo $date_string; ?>]" placeholder="Gross Sales % for <?php echo $date_string; ?>" />

Then the names of year_brand_gross[2013] and such would automatically get populated as an array into $_POST['year_brand_gross'], eliminating the need to loop through the POST input.

Instead you could set this to a variable like this

if(!empty($_POST['year_brand_gross']) {
    $year_array = $_POST['year_brand_gross'];
}

For PHP < 5.3.0 you can use alternate method to generate the year strings:

$current_date = new DateTime();
for ($i = 1; $i <=4; $i++) {
    $current_date->modify('-1 year');
    $date_string = $current_date->format('Y');
    // other code here
}

Note that, as shown, this will alter the value of $current_date with each iteratoin, which differs from the first solution.

4 Comments

When trying to implement your example I'm getting an error on the 3rd line of your first block of code.
What error are you getting? What version of PHP are you using, as DateInterval requires PHP >= 5.3.0?
Just getting a parse error. The version may be the problem... I know I'm running at least 5. I'll have to check and get back to you Monday. Really appreciate the help, Mike!
@ItsMIllerTime65 I updated the with an alternative approach for PHP < 5.3
0

First off you want to update you html so that the names of the fields

<input type="number" name="year[<?= date("Y")-4 ?>brand%gross]" placeholder="Gross Sales % for <?= date("Y")-4 ?>">
<input type="number" name="year[<?= date("Y")-3 ?>brand%gross]" placeholder="Gross Sales % for <?= date("Y")-3 ?>">
<input type="number" name="year[<?= date("Y")-2 ?>brand%gross]" placeholder="Gross Sales % for <?= date("Y")-2 ?>">
<input type="number" name="year[<?= date("Y")-1 ?>brand%gross]" placeholder="Gross Sales % for <?= date("Y")-1 ?>">

In your PHP you will be able to access the elements by there keys

$val2010 = $_POST["year"]["2010brand%gross"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.