gpt4 book ai didi

PHP 在 X 个 div 之后结束 div,并在 X 个 div 之后开始新的 div

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:32 25 4
gpt4 key购买 nike

我正在尝试使用 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这是我现在的页面,上面的三个 block 是正确的,我希望我的模板具有这种结构。但是我找不到一种方法来为我用循环生成的每个 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 } ?>

关于PHP 在 X 个 div 之后结束 div,并在 X 个 div 之后开始新的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20529217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com