繁体   English   中英

Wordpress 只在 for 循环中回显一次

[英]Wordpress echo something in for loop only once

所以我试图只打印一些标签一次,每个循环有四个标签。

所以基本上我试图循环并为每个标签打印一次标签。

在第二个时,它正在为每个循环打印四个标签。

为了更好地解释这里有一些图片。

我想要什么 - http://i.imgur.com/LPbqd53.png它目前是什么 - http://i.imgur.com/WqNrgBc.png

这是我的代码

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

这绝不是复制粘贴解决方案。 您的代码中有一些变量我不知道它们包含什么,但这应该作为解决方案的潜在想法。 本质上,所需的结果是数据的“表格”。 是否使用 table 或 divs 来构建 HTML 结构取决于您。 您可以使用诸如引导程序或基础或其他 CSS 框架提供的布局。 此示例仅使用一个表来提供结构。 您需要将答案标签绘制到一个容器中,例如用于在表头中迭代的数组。 然后,您可以简单地使用输入来呈现每个答案,而不必担心是否在每次迭代时打印它。 希望这可以帮助!

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

我设法使用重置解决了这个问题。

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

实际上您做错了,因为您正在使用输入打印标签。

你可以先打印静态标签,然后你应该打印为

foreach 输入显示内联

您还可以使用 table <tr> <td>正确对齐输入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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