dst3605528 2013-06-07 19:23 采纳率: 0%
浏览 70
已采纳

$ _POST ['']内的动态变量回显

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!

  • 写回答

3条回答 默认 最新

  • doukuibi9631 2013-06-07 19:43
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。