PHP结束后的PHP结束div,并在X div之后启动新的div

时间:2013-12-11 20:24:53

标签: php html css loops grid

我正在尝试使用php循环构建不对称网格。

我设法做到了:

<?php
$arr = array("blue", "yellow", "red", "pink", "green", "cyan", "gold", "orange", "magenta","blue", "yellow", "red", "pink", "green", "cyan", "gold", "orange", "magenta","blue", "yellow", "red", "pink", "green", "cyan", "gold", "orange", "magenta");

$i = 0;
foreach ($arr as $val) {

    if($i <= 4) {
        if($i%2 == 0) {
            if($i != 0) {
                echo "</div>";
            }
            echo '<div class="container" style="margin:20 10px;border:1px solid;">';
        }
    }
    if($i%7 == 0) {
        if($i != 0) {
            echo "</div>";
            echo '<div class="container" style="margin:20 10px;border:1px solid;">';
        }   
    }
    ?>
    <div class="holder" style="font-family:helvetica;font-weight:bold;padding:5px;background-color:<?php echo $val; ?>;">
        <?php echo $i." - ".$val;?>
    </div>
<?php
$i++;
}
?>
</div>

这就是我想要做的事情:

<div class="container">
<div class="holder"> 0 - blue </div>
<div class="holder"> 1 - yellow </div>
</div>
<div class="container">
<div class="holder"> 2 - red </div>
<div class="holder"> 3 - pink </div>
</div>
<div class="container">
<div class="holder"> 4 - green </div>
<div class="holder"> 5 - cyan </div>
<div class="holder"> 6 - gold </div>
</div>
<div class="container">
<div class="holder"> 0 - blue </div>
<div class="holder"> 1 - yellow </div>
</div>
<div class="container">
<div class="holder"> 2 - red </div>
<div class="holder"> 3 - pink </div>
</div>
<div class="container">
<div class="holder"> 4 - green </div>
<div class="holder"> 5 - cyan </div>
<div class="holder"> 6 - gold </div>
</div>
<div class="container">
<div class="holder"> 0 - blue </div>
<div class="holder"> 1 - yellow </div>
</div>
<div class="container">
<div class="holder"> 2 - red </div>
<div class="holder"> 3 - pink </div>
</div>
<div class="container">
<div class="holder"> 4 - green </div>
<div class="holder"> 5 - cyan </div>
<div class="holder"> 6 - gold </div>
</div>

更新:http://phpfiddle.org/main/code/ke2-ku8 这是我的页面,顶部的三个块是正确的,我希望我的模板具有这种结构。但我无法找到一种方法来为我用循环生成的每个div做到这一点。

任何建议都很棒!

由于

1 个答案:

答案 0 :(得分:0)

试试这个:

<?php // Fun testing array
$arr = array("blue", "yellow", "red", "pink", "green", "cyan", "gold", "orange", "magenta","blue", "yellow", "red", "pink", "green", "cyan", "gold", "orange", "magenta","blue", "yellow", "red", "pink", "green", "cyan", "gold", "orange", "magenta");

// Here's where we control the breaks
$breakpoints = array(0,2,4);
// Highest number of rows/items
$max_point = 7;

// Loop through all items
foreach ($arr as $i=>$val) {
    // Track breaks
    $calc = $i% $max_point;
    // Check for a breakpoint to make things happen
    if(in_array($calc,$breakpoints)) {
        // Is this a virgin?
        if ($i > 0) echo "</div>";
        // Output a new container
        echo '<div class="container" style="margin:20px 10px;border:1px solid;">'."\n\r";
    } ?>
    <div class="holder" style="font-family:helvetica;font-weight:bold;padding:5px;background-color:<?php echo $val; ?>;"><?php echo $i." - ".$val; $i++; ?></div>
<?php } ?>