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

もちろん、これはすべてを1行にキックします。行ごとに 3 つの grid-4 を生成するにはどうすればよいですか。

ありがとう!

4

3 に答える 3

3

index => valueプロパティを a で使用してforeach(またはループで完全に置き換えてfor)、インデックスに対して modulo を実行して、条件付きで行 div を挿入できます。であると仮定$entriesarrayます。

<?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 = 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 に答える
0

$entriesそれがインデックス付き配列であると仮定すると、が 0、3、6、および 9 の$ind%3場合は 0 になります。$ind

<?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 に答える