0

需要一些帮助来解决这个问题。我有一个这样生成的服务列表:

<div class="row">
    <?php foreach( $entries as $s ) : ?>
    <?php require( dirname(__FILE__) . '/_index_DisplayService.php' ); ?>

    <?php endforeach; ?>
</div>

_index_DisplayService.php

<div class="grid-4">
    <div id="service-mobile-dev" class="service">   
        <div class="service-icon">
            <i class="icon-calendar"></i>
        </div>
    </div>
</div>

现在这是一个网格 12 布局,所以我需要 3 个网格,每行需要 4 个网格,然后是新行。我认为解决方案首先要移动<div class="row">下面的内容,如下所示<?php foreach( $entries as $s ) : ?>

<?php foreach( $entries as $s ) : ?>
    <div class="row">
        <?php require( dirname(__FILE__) . '/_index_DisplayService.php' ); ?>
    </div>
<?php endforeach; ?>

这当然会将所有内容都踢到一行。我如何着手每行生成 3 个 grid-4。

谢谢!

4

3 回答 3

3

您可以使用 a 中的index => value属性foreach(或将其完全替换为for循环)并在索引上运行模数以有条件地插入行 div。假设$entries是一个array.

<?php
    foreach ($entries as $key => $value) :

        // Close the "previous" row tag before beginning the next row
        // Obviously we should not start with a closing tag ($key > 0)
        if ($key > 0 && $key%3 == 0) {
            ?>
            </div>
            <?php
        }

        // Start a new row
        if ($key%3 == 0) {
            ?>
            <div class="row">
            <?php
        }

        // Insert the "grid-4" elements
        require( dirname(__FILE__) . '/_index_DisplayService.php' );

    endforeach;

    // If there were any entries then we have a row which hasn't been closed yet.
    // Close it.
    if (count($entries) > 0) {
        ?>
            </div>
        <?php
    }
?>
于 2012-12-22T05:31:19.923 回答
0

假设这$entries是一个索引数组,$ind%3$ind为 0、3、6 和 9 时将为 0。

<?php foreach( $entries as $ind=>$s ) : ?>
    <?if($ind%3==0){?><div class="row"><?}?>
        <?php require( dirname(__FILE__) . '/_index_DisplayService.php' ); ?>
    <?if($ind%3==0){?></div><?}?>
<?php endforeach; ?>
于 2012-12-22T05:33:19.633 回答
0

为此,您需要设置一个计数器。我添加了一个代表您的条目的假数组,因此您可以按原样对其进行测试。当我写这篇文章时,看起来其他一些人也发布了一些非常好的答案。祝你好运!

    $entries = array(
        'entry-one',
        'entry-two',
        'entry-three',
        'entry-four',
        'entry-five',
        'entry-six'
);

$counter = 0;
foreach( $entries as $s ) {
    if( $counter > 2 ) {$counter = 0;}
    if( $counter === 0 ) {
        echo '<div class="row">' ;
    }

    echo '
    <div class="grid-4">
        <div id="service-mobile-dev" class="service">   
            <div class="service-icon">
                <i class="icon-calendar"></i>'
                . $s .'
            </div>
        </div>
    </div>';

    if( $counter === 2) {
        echo "</div>";
    }
    $counter++;
} 
于 2012-12-22T06:05:54.560 回答