0

This is a difficult question to explain so bear with me...

I need to output a form wizard based on the following array:

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
    )

)

The qb-type => ttl field is a title field and also a new page in the form wizard the output would be as follows for each title:

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

so the output from the above array would be...

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

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

My question is how can I display anything below the title field in the .content area for example:

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

I've managed to out put the title but I'm not sure how I can add the sub fields to each section:

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

I hope I've managed to explain everything well enough, but please feel free to ask if you need a better explanation.

EDIT Any data directly below the title until you reach the next title would be considered part of the ttl section

4
  • I do not understand where the data to be displayed are when the qb-type is equal to "ttl". You do not provided any or any way to understand where which array elements are to be considered part of the ttl member Commented Nov 18, 2015 at 14:20
  • Sorry @RiccardoC, I don't understand your question, could you explain? Commented Nov 18, 2015 at 14:21
  • Any data directly below the title until you reach the next title would be considered part of the ttl section Commented Nov 18, 2015 at 14:23
  • Undestood... I thnk it's not a good design, but I'll think about it Commented Nov 18, 2015 at 14:24

1 Answer 1

2

Try this one

<?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>
Sign up to request clarification or add additional context in comments.

2 Comments

Works like a charm! Thank you very much!!
I added a </div> that was missing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.