bootstrap php 多行,使用PHP循环将Bootstrap行和正确的列号添加到元素

我喜欢你的问题,因为我正在处理一个非常相似的情况.由于其他答案有点长,我决定把我的留在这里供你考虑.对我来说,你使用的变量越少,解决方案就越好.

BootstrapContentArranger.PHP

function BootstrapContentArrange($i) {

$items = $i; // qnt of items

$rows = ceil($items/3); // rows to fill

$lr = $items%3; // last row items

$lrc = $lr; // counter to last row

while ($items > 0) { // while still have items

$cell = 0;

if ($rows > 1) { // if not last row...

echo '

'.PHP_EOL;

while ($cell < 3) { // iterate with 3x4 cols

echo '

Content
'.PHP_EOL;

$cell++;

}

echo "

".PHP_EOL;

$rows--; // end a row

} elseif ($rows == 1 && $lr > 0) { // if last row and still has items

echo '

'.PHP_EOL;

while ($lrc > 0) { // iterate over qnt of remaining items

$lr == 2 ? // is it two?

print('

Content
'.PHP_EOL) : // makes 2x6 row

print('

Content
'.PHP_EOL); // makes 1x12 row

$lrc--;

}

echo "

".PHP_EOL;

break;

} else { // if round qnt of items (exact multiple of 3)

echo '

'.PHP_EOL;

while ($cell < 3) { // iterate as usual

echo '

Content
'.PHP_EOL;

$cell++;

}

echo "

".PHP_EOL;

break;

}

$items--; // decrement items until it's over or it breaks

}

}

测试用例

BootstrapContentArrange(3);

BootstrapContentArrange(11);

BootstrapContentArrange(1);

> 3项,输出:

Content
Content
Content

> 11项,产出:

Content
Content
Content
Content
Content
Content
Content
Content
Content
Content
Content

>单个项目,输出:

Content

注意:你可以删除PHP_EOL,我用它来更好地阅读源代码.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值