【问题标题】:PHP simple for loop within HTML (Codeigniter)HTML中的PHP简单for循环(Codeigniter)
【发布时间】:2016-05-08 01:24:55
【问题描述】:

我希望有人可以帮助我解决这个问题。我使用 codeigniter 作为我的框架。在我的一个观点中,我想输出与数据库中“bestoutof”列中的数字一样多的选项卡。我试过的代码是:

<?php for ($x = 1; $x = $post->bestoutof; $x++) {
echo '<div class="tab-pane fade active in" id="game<?php $post->bestoutof; ?>"></div>'; } ?>

但它似乎不起作用。非常感谢您对此提供任何帮助。

谢谢!

【问题讨论】:

  • 它有什么作用?有什么错误吗? $post->bestoutof 的值是多少?
  • $x = $post->bestoutof; ???。如果“bestoutof”不是 1,则不会执行循环。使用 $x bestoutof;
  • @Peter $post->bestoutof 的价值是多少?

标签: php html codeigniter loops for-loop


【解决方案1】:

对于 for 循环内的 Id,您需要使用 $x,如下例所示:

修改后的代码:

<?php
for ($x = 1; $x = $post['bestoutof']; $x++) { 
    ?>
    <div class="tab-pane fade active in" id="game<?php echo $x;?>"></div>
    <?php
}
?>

注意:如果$post['bestoutof'] 有多个值,则$x = $post['bestoutof'] 值应添加小于标记$x &lt;= $post['bestoutof']。这将取决于您对它的价值

每次迭代都使用$post-&gt;bestoutof 的代码有什么问题。这将返回相同的游戏 ID 未更改。

并且不要在php标签内使用php标签。

【讨论】:

    【解决方案2】:

    您在 PHP 标签内有 PHP 标签。您将把代码输出作为文字字符串。正如@devpro 指出的那样,您可能希望使用计数器作为您的 id,否则,您将拥有重复的 id。

    <?php for ($x = 1; $x = $post->bestoutof; $x++) {
      echo '<div class="tab-pane fade active in" id="game'. $x .'"></div>'; 
    } ?>
    

    【讨论】:

      【解决方案3】:

      $x = $post->bestoutof;这决定了迭代的次数。

      其实应该是$x bestoutof;

      <?php for ($x = 1; $x <= $post->bestoutof; $x++) {
      echo '<div class="tab-pane fade active in" id="game<?php $post->bestoutof; ?>"></div>'; } ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-15
        • 2017-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多