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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里