我希望有人能帮我解决这个问题。我使用 codeigniter 作为我的框架。在我的一个观点中,我想输出与数据库中“bestoutof”列中的数字一样多的选项卡。我试过的代码是:
<?php for ($x = 1; $x = $post->bestoutof; $x++) {
echo '<div class="tab-pane fade active in" id="game<?php $post->bestoutof; ?>"></div>'; } ?>
但是好像不行。如果能提供任何帮助,我将不胜感激。
谢谢!
最佳答案
对于 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
}
?>
Note: If
$post['bestoutof']
have multiple values, then$x = $post['bestoutof']
value should be added less-than mark$x <= $post['bestoutof']
. This will depend with what the value you have on it
您使用的代码有什么问题 $post->bestoutof
对于每次迭代。这将返回相同的游戏 ID 未更改。
并且不要在 php 标签内使用 php 标签。
关于HTML 中的 PHP 简单 for 循环 (Codeigniter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35099272/