我正在尝试用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都可以做到的方法。

任何建议都很棒!

谢谢

最佳答案

尝试这个:

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

10-07 18:06