在循环中构建 HTML

发布于 2024-12-09 04:10:52 字数 4806 浏览 0 评论 0原文

我试图在 PHP 中循环一个数组,同时创建一些 HTML 来输出。

当循环第一次开始时,我需要输出

  • 然后我需要添加 3 div 然后关闭
  • 并再次启动该过程。
  • 我已经尝试过这个,

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

    但是我所有的 li 都获得了类行。

    期望的输出:

    <li class="row">
                <div class="grid_8">
                    <div class="candidate">
                        <div class="model_image shadow_50"></div>
                        <dl>
                            <dt>Jessica Womersley</dt>
                            <dd>
                                actress &amp; presenter
                                <ul>
                                    <li><a href="" class="details">View Details</a></li>
                                    <li><a href="" class="showreel">Showreel</a></li>
                                    <li><a href="" class="shortlist">Shortlist</a></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
                <div class="grid_8">
                    <div class="candidate">
                        <div class="model_image shadow_50"></div>
                        <dl>
                            <dt>Jessica Womersley</dt>
                            <dd>
                                actress &amp; presenter
                                <ul>
                                    <li><a href="" class="details">View Details</a></li>
                                    <li><a href="" class="showreel">Showreel</a></li>
                                    <li><a href="" class="shortlist">Shortlist</a></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
                <div class="grid_8">
                    <div class="candidate end">
                        <div class="model_image shadow_50"><span class="banner"></span></div>
                        <dl>
                            <dt>Jessica Womersley</dt>
                            <dd>
                                actress &amp; presenter
                                <ul>
                                    <li><a href="" class="details">View Details</a></li>
                                    <li><a href="" class="showreel">Showreel</a></li>
                                    <li><a href="" class="shortlist">Shortlist</a></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
            </li>
    

    I am trying to loop through an array in PHP and at the same time, create some HTML to output.

    When the loop first starts I need to output <li class="row"> I then need to add 3 divs then close the <li> and start the process again.

    I have tried this,

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

    however all my li get the class row.

    Desired Output:

    <li class="row">
                <div class="grid_8">
                    <div class="candidate">
                        <div class="model_image shadow_50"></div>
                        <dl>
                            <dt>Jessica Womersley</dt>
                            <dd>
                                actress & presenter
                                <ul>
                                    <li><a href="" class="details">View Details</a></li>
                                    <li><a href="" class="showreel">Showreel</a></li>
                                    <li><a href="" class="shortlist">Shortlist</a></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
                <div class="grid_8">
                    <div class="candidate">
                        <div class="model_image shadow_50"></div>
                        <dl>
                            <dt>Jessica Womersley</dt>
                            <dd>
                                actress & presenter
                                <ul>
                                    <li><a href="" class="details">View Details</a></li>
                                    <li><a href="" class="showreel">Showreel</a></li>
                                    <li><a href="" class="shortlist">Shortlist</a></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
                <div class="grid_8">
                    <div class="candidate end">
                        <div class="model_image shadow_50"><span class="banner"></span></div>
                        <dl>
                            <dt>Jessica Womersley</dt>
                            <dd>
                                actress & presenter
                                <ul>
                                    <li><a href="" class="details">View Details</a></li>
                                    <li><a href="" class="showreel">Showreel</a></li>
                                    <li><a href="" class="shortlist">Shortlist</a></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
            </li>
    

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

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

    发布评论

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

    评论(1

    笑着哭最痛 2024-12-16 04:10:53

    你把事情搞得太复杂了。为什么不在 foreach 中使用 for 循环呢?
    像这样

        <?php foreach ($results as $k => $v) : ?>
            <li class="row">
            <?php for($count = 1; $count <= 3; $count++) : ?>
    
                <div class="grid_8">
                    <div class="<?php if($count == 3) echo "candidate_end"; else echo "candidate";?>">
                        <div class="model_image shadow_50"></div>
                        <dl>
                            <dt><?php echo $v['first_name']; ?> <?php echo $v['surname']; ?></dt>
                            <dd>
                                <?php echo $v['talent']; ?>
                                <ul>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Details", array('class' => 'details')); ?></li>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Showreel", array('class' => 'showreel')); ?></li>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "Shortlist", array('class' => 'shortlist')); ?></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
            <?php endfor; ?>
            </li>
        <?php endforeach; ?>
    

    You are overcomplicating things. ¿Why not just use a for loop inside the for each?
    Like this

        <?php foreach ($results as $k => $v) : ?>
            <li class="row">
            <?php for($count = 1; $count <= 3; $count++) : ?>
    
                <div class="grid_8">
                    <div class="<?php if($count == 3) echo "candidate_end"; else echo "candidate";?>">
                        <div class="model_image shadow_50"></div>
                        <dl>
                            <dt><?php echo $v['first_name']; ?> <?php echo $v['surname']; ?></dt>
                            <dd>
                                <?php echo $v['talent']; ?>
                                <ul>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Details", array('class' => 'details')); ?></li>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Showreel", array('class' => 'showreel')); ?></li>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "Shortlist", array('class' => 'shortlist')); ?></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
            <?php endfor; ?>
            </li>
        <?php endforeach; ?>
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文