简体   繁体   中英

Wordpress echo something in for loop only once

So I'm trying to print some labels only once, there are four labels per loop.

So esentially i'm trying to loop through and print the labels once per label.

At the second it is printing the four labels per loop.

To better explain here are some pictures.

What I want - http://i.imgur.com/LPbqd53.png What it currently is - http://i.imgur.com/WqNrgBc.png

Here is my code

  <div class="wpt_test fill_form">

    <div class="answers">
      <div class="content">
        <form
        <?php foreach ($formAttributes as $key => $value):?>
          <?php echo $key ?>="<?php echo htmlspecialchars(is_array($value) ? json_encode($value) : $value) ?>"
        <?php endforeach; ?>>
        <?php if ($isShowContent): ?><div class="content" style="margin:0;"><?php echo $content ?></div><?php endif ?>    
        <?php if ($shortDescription): ?><div class="short-description"><?php echo $wp->autoParagraphise($shortDescription) ?></div><?php endif ?>
        <?php $wp->doAction('wp_testing_template_fill_form_questions_before') ?>                            
          <?php foreach($questions as $q => $question): /* @var $question WpTesting_Model_Question */ ?>
            <?php $wp->doAction('wp_testing_template_fill_form_question_before', $question, $q) ?>
          <div class="question">      
              <div class="title">
              <span class="question-title" style="width:100%;"><?php echo $question->getTitle() ?>
                <?php $wp->doAction('wp_testing_template_fill_form_label_end', array('required' => true)) ?></span>
                <?php if (!$isMultipleAnswers): ?>
                  <input type="hidden" name="<?php echo $answerIdName ?>[<?php echo $answerIndex ?>]" value="" />
                <?php endif ?>
              </div>
              <div class="answer-block">
                <?php foreach($question->buildAnswers() as $a => $answer): /* @var $answer WpTesting_Model_Answer */ ?>
                  <?php $answerId = 'wpt-test-question-' . $question->getId() . '-answer-' . $answer->getId() ?>
                  <div class="question-titles">
                    <div class="question-labels">
                    <label for="<?php echo $answerId ?>">
                        <input type="<?php echo $isMultipleAnswers ? 'checkbox' : 'radio' ?>" id="<?php echo $answerId ?>"
                            data-errormessage="<?php echo $isMultipleAnswers
                                ? __('Please select at least one answer.', 'wp-testing')
                                : __('Please choose only one answer.', 'wp-testing') ?>"
                            <?php if (0 == $a): ?>required="required" aria-required="true"<?php endif ?>
                            name="<?php echo $answerIdName ?>[<?php echo $answerIndex ?>]" value="<?php echo $answer->getId() ?>" />
                        <?php echo $answer->getTitleOnce() ?>
                    </label>     
                    </div>  
                  </div>  
                  <?php if ($isMultipleAnswers) {$answerIndex++;} ?>
                <?php endforeach ?>
              </div>
              <?php $wp->doAction('wp_testing_template_fill_form_question_after', $question, $q) ?>
              <?php if (!$isMultipleAnswers) {$answerIndex++;} ?>
            <?php endforeach ?>
          </div>

          <?php $wp->doAction('wp_testing_template_fill_form_questions_after') ?>

          <?php if($isFinal): ?>
            <p>
              <input type="submit" class="button" value="<?php echo $submitButtonCaption ?>" />
              <?php if($stepsCounter): ?><span class="steps-counter"><?php echo $stepsCounter ?></span><?php endif ?>
            </p>
          <?php else: ?>
            <div class="wpt_warning">
              <h4><?php echo __('Test is under construction', 'wp-testing') ?></h4>
              <p><?php echo __('You can not get any results from it yet.', 'wp-testing') ?></p>
            </div>
          <?php endif ?>
          <?php foreach($hiddens as $name => $value): ?><input type="hidden" name="<?php echo htmlspecialchars($name) ?>" value="<?php echo htmlspecialchars($value) ?>" /><?php endforeach ?>
        </form>
      </div>
    </div>

  </div>

By no means is this a copy-paste solution. There are variables in your code that I don't know what they contain but this should serve as a potential idea for the solution. Essentially the desired result is a "table" of data. Whether you use a table or divs to construct the HTML structure is on you. You could use a layout such as those provided by bootstrap or foundation or another CSS framework. This example simply uses a table to provide the structure. You will want to draw your answer labels out into a container such as an array for iteration in the table head. You may then present each answer with simply the input and not concern yourself with whether or not to print it on each iteration. Hope this helps!

<?php
/**
 * @var array $answerLabels
 * @var WpTesting_Model_Question[] $questions
 */
?>
<table>
    <thead>
        <tr>
            <td></td>
            <?php foreach ($answerLabels as $label): ?>
                <td><?= $label ?></td>
            <?php endforeach; ?>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($questions as $question): ?>
            <tr>
                <td><?= $question->getTitle() ?></td>
                <?php foreach ($question->buildAnswers() as $index =>     $answer):
                    $answerId = 'wpt-test-question-' . $question->getId() . '-answer-' . $answer->getId(); 
                ?>
                    <td>
                        <label for="<?= $answerId ?>">
                            <input type="<?= $isMultipleAnswers ? 'checkbox' : 'radio' ?>"    
                                   id="<?= $answerId ?>"
                                   data-errormessage="<?= $isMultipleAnswers
                                       ? __('Please select at least one answer.', 'wp-testing')
                                       : __('Please choose only one answer.', 'wp-testing') ?>"
                                <?php if (0 == $index): ?>
                                    required="required"
                                    aria-required="true"
                                <?php endif ?>
                                   name="<?= $answerIdName ?>[<?= $answerIndex ?>]"
                                   value="<?= $answer->getId() ?>"/>
                        </label>
                    </td>
                <?php endforeach; ?>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

I managed to fix this using a reset.

<?php if($i==0): ?>

<?php foreach($question->buildAnswers() as $a => $answer): /* @var $answer WpTesting_Model_Answer */ ?>

<?php $answerId = 'wpt-test-question-' . $question->getId() . '-answer-' . $answer->getId() ?>

<label for="<?php echo $answerId ?>"> <?php echo $answer->getTitleOnce(); ?></label>

<?php if ($isMultipleAnswers) {$answerIndex++;} ?>

<?php endforeach ?>

<?php $i = 1; ?>

<?php endif; ?>

Actually you are doing wrong, because you are printing the label with input.

you may first print the label static and then you should print as

foreach input display inline

you can also use table <tr> <td> for properly align the inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM