复杂的数组循环输出

时间:2015-11-18 14:13:50

标签: php arrays loops

这是一个难以解释的问题,请耐心等待......

我需要根据以下数组输出表单向导:

Array
(
[about_you] => Array
    (
        [qb-name] => about_you
        [qb-label] => About You
        [qb-type] => ttl
    )

[your_name] => Array
    (
        [qb-name] => your_name
        [qb-label] => What is your name?
        [required] => required
        [qb-type] => slt
    )

[website] => Array
    (
        [qb-name] => website
        [qb-label] => What is your current website?
        [required] => 
        [qb-type] => slt
    )

[your_requirements] => Array
    (
        [qb-name] => your_requirements
        [qb-label] => Your Requirements
        [qb-type] => ttl
    )

[hosting] => Array
    (
        [qb-name] => age
        [qb-label] => How old are you?
        [sq-option] => Array
            (
                [0] => 0-20
                [1] => 21-40
                [2] => 40+
            )

        [qb-type] => sel
    )

[likes] => Array
    (
        [qb-name] => likes
        [qb-label] => What do you like?
        [required] => required
        [qb-type] => mlt
    )

)

qb-type => ttl字段是标题字段,也是表单向导中的新页面,每个标题的输出如下:

<div id="wizard">
    <!-- foreach.... -->
    <h1> [qb-label] </h1>
    <div class="content"></div>
    <!-- /foreach -->
</div>

所以上面数组的输出是......

&#13;
&#13;
<div id="wizard">
      <h1>About You</h1>
      <div class="content"></div>

      <h1>Your Requirements</h1>
      <div class="content"></div>
   </div>
&#13;
&#13;
&#13;

我的问题是如何在.content区域的标题字段下方显示任何内容,例如:

&#13;
&#13;
<div id="wizard">
          <h1>About You</h1>
          <div class="content">
            <p>
              <label>What is your name?</label>
              <input name="your_name" />
             </p>
            <p>
              <label>What is your current website?</label>
              <input name="your_name" />
             </p>
          </div>

          <h1>Your Requirements</h1>
          <div class="content">
             How old are you?
             What do you like?
          </div>
       </div>
&#13;
&#13;
&#13;

我设法将标题输出,但我不确定如何将子字段添加到每个部分:

<div id="wizard">
<?php foreach($form as $field=>$option): ?>
    <?php if($option['qb-type'] == 'ttl'): ?>
        <h1><?= $option['qb-label'] ?></h1>
        <div>The content goes here</div>
    <?php endif; ?>
<?php endforeach; ?>
</div>

我希望我能够很好地解释一切,但请随时询问您是否需要更好的解释。

编辑在您到达下一个标题之前,标题正下方的任何数据都将被视为ttl部分的一部分

1 个答案:

答案 0 :(得分:2)

试试这个

<?php $first = true; ?>
<?php foreach( $form as $field=>$options ) { ?>
    <?php if( $options[ "qb-type" ] == "ttl" ) { ?>
        <?php if( !$first ) echo "</div>";?>
        <?php $first = false; ?>
        <h1><?= $options['qb-label'] ?></h1><div>
    <?php } else { ?>
        <p>
            <label><?= $options["qb-label"]?></label>
            <input type="text" name="<?= $options[ "qb-name"]?>">
        </p>
    <?php }?>

<?php } ?>
</div>