PHP在X个div之后结束div,并在X个div之后开始新的div
我正在尝试用php循环构建一个非对称网格 我已设法做到这一点:PHP在X个div之后结束div,并在X个div之后开始新的div,php,html,css,loops,grid,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"
<?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>
试试这个:
<?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 } ?>
您到底想做什么?您当前的代码在哪里失败?我建议您使用类和子选择器。看起来您正试图根据输出的div更改CSS。这将使您的代码更干净,更易于管理。我建议您停止使用style属性。这是一个示例。。。当然,我使用类break
和continue
可以让你控制foreach循环的流程。谢谢,我已经更新了帖子
<?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 } ?>