【问题标题】:Complicated array loop output复杂的数组循环输出
【发布时间】:2015-11-18 14:13:49
【问题描述】:

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

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

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>

所以上述数组的输出将是......

<div id="wizard">
      <h1>About You</h1>
      <div class="content"></div>

      <h1>Your Requirements</h1>
      <div class="content"></div>
   </div>

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

<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>

我已经把标题写出来了,但我不确定如何将子字段添加到每个部分:

<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 部分的一部分

【问题讨论】:

  • 不明白qb-type等于“ttl”时要显示的数据在哪里。您没有提供任何或任何方式来了解哪些数组元素将被视为 ttl 成员的一部分
  • 对不起@RiccardoC,我不明白你的问题,你能解释一下吗?
  • 在您到达下一个标题之前,任何直接位于标题下方的数据都将被视为 ttl 部分的一部分
  • Undestood...我认为这不是一个好的设计,但我会考虑一下

标签: php arrays loops


【解决方案1】:

试试这个

<?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>

【讨论】:

  • 像魅力一样工作!非常感谢!!
  • 我添加了一个丢失的
猜你喜欢
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多