在循环中构建HTML

时间:2011-10-11 16:09:45

标签: php codeigniter loops foreach

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

当循环首次启动时,我需要输出<li class="row">然后我需要添加3 divs然后关闭<li>并重新开始此过程。

我试过了,

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

1 个答案:

答案 0 :(得分:1)

你太复杂了。 ¿为什么不在每个内部使用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; ?>