循环数组问题并确定何时输入 HTML

发布于 2024-12-10 17:38:32 字数 1880 浏览 0 评论 0原文

我正在尝试循环遍历一个数组,每 3 个循环创建一个新行,但是我很难让它工作,目前我的代码如下所示,

<li class="row">
        <?php for ($i = 0; $i < count($results); $i++) : ?>
                <div class="grid_8">
                    <div class="candidate <?php if ($i % 3 == 2) echo "end"; ?>">
                        <div class="model_image shadow_50"></div>
                        <dl>
                            <dt><?php echo $results[$i]['first_name']; ?> <?php echo $results[$i]['surname']; ?></dt>
                            <dd>
                                <?php echo $results[$i]['talent']; ?>
                                <ul>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "View Details", array('class' => 'details')); ?></li>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "View Showreel", array('class' => 'showreel')); ?></li>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "Shortlist", array('class' => 'shortlist')); ?></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
            <?php if ($i % 3 == 3) : ?>
                 </li><li class="row">   
            <?php endif; ?>
        <?php endfor; ?>

但是这只是创建一行,并且我的所有结果都在其中,而它应该是 1 里,包含一类行,然后是 3 个 .grid_8 div,然后是另一行。

我哪里出错了?

I am trying to loop through an array, and every 3 loops create a new row, however I am having difficulty getting it to work, currently my code looks like this,

<li class="row">
        <?php for ($i = 0; $i < count($results); $i++) : ?>
                <div class="grid_8">
                    <div class="candidate <?php if ($i % 3 == 2) echo "end"; ?>">
                        <div class="model_image shadow_50"></div>
                        <dl>
                            <dt><?php echo $results[$i]['first_name']; ?> <?php echo $results[$i]['surname']; ?></dt>
                            <dd>
                                <?php echo $results[$i]['talent']; ?>
                                <ul>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "View Details", array('class' => 'details')); ?></li>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "View Showreel", array('class' => 'showreel')); ?></li>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($results[$i]['first_name']) . "-" . strtolower($results[$i]['surname']), "Shortlist", array('class' => 'shortlist')); ?></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
            <?php if ($i % 3 == 3) : ?>
                 </li><li class="row">   
            <?php endif; ?>
        <?php endfor; ?>

However this just creates one row, and with all my results in it, whereas it should be 1 li with a class of row, and then 3 .grid_8 divs and then another row.

Where am i going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哭了丶谁疼 2024-12-17 17:38:32

问题是你的模量方程。余数永远不会达到 3,并且将是 (0,1,2,0,1,2,0,1,2) 的模式。所以你把它改为等于2。

        if ($i % 3 == 2)

The issue is your modulus equation. The remainder will never reach 3, and will be a pattern of (0,1,2,0,1,2,0,1,2). So you change it to equal 2.

        if ($i % 3 == 2)
铁憨憨 2024-12-17 17:38:32

如果您使用计数器来跟踪循环中的行位置,可能会让事情变得更容易。

$count = 0;
for ($i = 0; $i < count($results); $i++) {
    if ($count == 0) {
        echo "<li class=\"row\">";
    }

    echo "all your middle stuff";
    $count++;

    if ($count == 4) {
        echo "</li>";
        $count = 0;
    }
}

//just make sure we dont need to add a /li
if ($count % 3 != 0) 
    echo "</li>

If you use a counter to keep track of where you are row wise in your loop it may make things easier.

$count = 0;
for ($i = 0; $i < count($results); $i++) {
    if ($count == 0) {
        echo "<li class=\"row\">";
    }

    echo "all your middle stuff";
    $count++;

    if ($count == 4) {
        echo "</li>";
        $count = 0;
    }
}

//just make sure we dont need to add a /li
if ($count % 3 != 0) 
    echo "</li>
三生殊途 2024-12-17 17:38:32

我只能假设你有一个封闭的

    somewhere that you haven't shown us. Nonetheless it appears as if you're missing your final so your browser is confused.

    I can only presume you have an enclosing

      somewhere that you haven't shown us. Nonetheless it appears as if you're missing your final

      so your browser is confused.

      请帮我爱他 2024-12-17 17:38:32

      它不能像这样 if ($i % 3 == 3) - $i % 3 始终在 0..2 范围内。
      也许你 if ($i % 3 == 0) - 它将是“每三个 $i”

      It just cant be like this if ($i % 3 == 3) - $i % 3 is always in range 0..2.
      Maybe you if ($i % 3 == 0) - it will be "every third $i"

      ~没有更多了~
      我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
      原文