Foreach循环在3列div中,每列中都有嵌套的div

时间:2011-02-25 09:59:52

标签: php loops foreach

在此脚本的其余部分中需要一些线索:

// Num of columns
$col = 3;
foreach ($schema as $key => $array) {

// wrapper div
if($key % $col == 0) {
    echo '<div class="wrapper" style="float: left;">;
}

// divs inside columns  
foreach ($array as $c => $value) {
    echo '<div>' . $value . '</div>';
}

// Here I need to close wrapper div
// @ to do
}

1 个答案:

答案 0 :(得分:1)

// Num of columns
$col = 3;
echo '<div class="wrapper" style="float: left;">';
$count = 1;

foreach ($schema as $key => $array) {

    // wrapper div
    if($count % $col == 0) {
       echo '</div>';

       if(isset($schema[$key+1]))   // check if next element exists then start new div
           echo '<div class="wrapper" style="float: left;">';
   }

   // divs inside columns  
   foreach ($array as $c => $value) {
       echo '<div>' . $value . '</div>';
   }

   $count++;
}

if(count($schema) % $col != 0)
   echo '</div>'; // at last close div