dongmaopan5738 2013-04-24 11:01
浏览 88
已采纳

PHP使用POST信息创建表,循环遍历填充的变量

I have the following post variables:

$line_1_09
$line_1_12
$line_1_15
$line_1_18
$line_1_21
$line_2_09
$line_2_12
$line_2_15
$line_2_18
$line_2_21
$line_3_09
$line_3_12
$line_3_15
$line_3_18
$line_3_21

I know from my previous form that of the 15 inputs(posts) 12 are populated. the 12 is stored in variable $populatedrows.

I then want to create a table on my new page

<table>
<?php for ($i=1; $i<=$populatedrows; $i++)
  {
?>
    <tr>
       <td>
         <input type="text" value="//first post with information//">
       </td>
    </tr>
<?php } ?>
</table>

so in this example, if $line_1_09 and $line_1_12 are empty, the first table row input must then be $line_1_15

and so it will continue 'looping' through the next available / populated post variables until the table is equal to $populatedrows. this will be equal to the number of post variables that contain data.

Strange situation for me so not really sure how to go about it.

  • 写回答

4条回答 默认 最新

  • douju6542 2013-04-24 11:26
    关注

    In case you just want to create input for each not empty $_POST variable:

        <?php
        foreach($_POST as $key => $value) //$key is e.g 'line_1_20'
        {
            // substr($key, 0, 5) == 'line_' checks if the $key starts with 'line_'
            if((substr($key, 0, 5) == 'line_') && !empty($value))
            {
            ?>
                <tr>
                    <td>
                      <input type="text" value="<?php echo $value ?>">
                    </td>
                </tr>
            <?php
            }
        }
        ?>
    

    If you want less then all populated $_POST:

        <?php
        $count = 0; //count rendered fields
        foreach($_POST as $value)
        {
            if(!empty($value))
            {
            ?>
                <tr>
                    <td>
                      <input type="text" value="<?php echo $value ?>">
                    </td>
                </tr>
            <?php
            $count++; //increase counter
            }
    
            if($count == $populatedrows) //if the coutner hits the requested amount break the loop
                break;
        }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集