【问题标题】:Show different number of column per row in foreach loop在 foreach 循环中每行显示不同的列数
【发布时间】:2020-10-18 14:12:20
【问题描述】:

我从数据库中获取了一些目的地城市列表,并希望将其显示在视图中。

这是我的正常做法

<?php
$this->db->select('*');
$citydata = $this->db->get('cities')->result_array();

foreach ($citydata as $citydatas){?>

<div class="col-lg-4">
<img src="<?=$citydatas['banner];?>" />
</div>

<? } ?>

上面的代码每行会显示3列,

但我想要的是不同的方法,我需要在第一行显示 2 列,然后在第二行显示 3 列。

另一种方法是 - 第一行三列,但第一列将占用一半屏幕,其他两列将占用另一半,在第二行中,前两列占用一半,然后第三列使用 col-6 占用其余半屏

这是我正在寻找的第一个网格样式

这是第二种网格样式

我的第一种风格的方法,下面的方法好用吗?

<?php
$this->db->select('*');
$citydata = $this->db->get('cities')->result_array();

$i = 1;

foreach ($citydata as $citydatas){?>

<div class="<?php if($i++ == 1 || $i++ == 2){ echo "col-lg-6";}else{echo"col-lg-4";}?>">
<img src="<?=$citydatas['banner];?>" />
</div>

<? } ?>

【问题讨论】:

  • 你需要 CSS 和 JS。
  • 需要在第一行显示 2 列,然后在第二行显示 3 列。和第一行的三列 - 不确定第一行需要多少列...
  • @Yousaf - 我已经纠正了这个问题,我已经分离了两种方法,并且我已经为我想要的两个目标粘贴了两个不同的图像。我希望它可以消除你的困惑。
  • @SalimIbrogimov :你能给我一些示例代码吗?我在我的模板中使用 bootstrap4。
  • @Happy Voyaging 你只想要两个或更多的网格样式?

标签: php html css twitter-bootstrap foreach


【解决方案1】:

您可以使用nth-child 选择器和 CSS 网格来获得所需的结果。

第一个网格样式

.grid-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 10px;
  grid-auto-rows: 200px;
}

img:nth-child(6n + 1),
img:nth-child(6n + 2){
  grid-column: span 2;
}

img {
  width: 100%;
  height: 100%;
}
<div class="grid-container">
  <img src="https://picsum.photos/id/10/400" />
  <img src="https://picsum.photos/id/20/400" />
  <img src="https://picsum.photos/id/30/400" />
  <img src="https://picsum.photos/id/40/400" />
  <img src="https://picsum.photos/id/50/400" />
  <img src="https://picsum.photos/id/60/400" />
</div>

第二种网格样式

对于第二种网格样式,您只需更改nth-child 选择器,其余代码将相同。

img:nth-child(6n),
img:nth-child(6n + 1) {
  grid-column: span 2;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 10px;
  grid-auto-rows: 200px;
}

img:nth-child(6n),
img:nth-child(6n + 1) {
  grid-column: span 2;
}

img {
  width: 100%;
  height: 100%;
}
<div class="grid-container">
  <img src="https://picsum.photos/id/10/400" />
  <img src="https://picsum.photos/id/20/400" />
  <img src="https://picsum.photos/id/30/400" />
  <img src="https://picsum.photos/id/40/400" />
  <img src="https://picsum.photos/id/50/400" />
  <img src="https://picsum.photos/id/60/400" />
</div>

PHP 代码

使用以下代码使用 PHP 呈现此布局

<div class="grid-container">
  <?php foreach($citydata as $citydatas): ?>
    <img src="<?=$citydatas['banner]?>"/>
  <?php endforeach; ?>
</div>

【讨论】:

  • 我已经使用引导程序创建了该模板,但是如何在 foreach 循环中实现相同的设计或布局,这是我的问题。
  • 如果我遵循你的方法,那么我如何在 foreach 循环的第一项和第六项中添加 span2 类。
  • @Yousaf 先生,用户使用php foreach loop 来显示图像结果,那么他会做什么?
  • @Yousaf :您的回答很有帮助,但是由于我已经在使用 Bootstrap 类,使用迭代并根据迭代值为每个结果分配类是否很好,就像我在我的问题中更新的那样。
  • 在这种情况下,使用nth-child选择器比有条件地添加类要好
猜你喜欢
  • 2019-09-22
  • 2015-05-21
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
  • 2022-06-22
  • 2016-02-06
  • 1970-01-01
  • 2013-05-18
相关资源
最近更新 更多