首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从上到下拆分2列数据?

如何从上到下拆分2列数据?
EN

Stack Overflow用户
提问于 2019-12-18 18:07:23
回答 2查看 617关注 0票数 1

我有一个关于在Bootstrap中从上到下显示2列数据的方法的问题。我的数据在一个数组中,所以我用foreach循环它。

在附加的图片上,你可以看到我打算以垂直然后水平的方式排列我的两列,所以项目的前半部分应该在左列,后半部分在右列。

这段代码

代码语言:javascript
复制
<div class="container bg-grey pt-4 pb-4">
<h4 class="text-secondary">Buy</h4>
<div class="row">
    <div class="col-6 col-lg-6 col-xs-6 col-md-6 col-sm-6">
        <div class="row">
            <?php foreach ($product as $k => $v) : ?>
            <div class="col-12 pr-1">
                <div class="swiper-slide mb-2" style="height: unset">
                    <div class="card border-0" style="width: 18rem;">
                        <img class="card-img-top rounded-0"
                             src="himg/products/j01l.jpg"
                             alt="Card image cap">
                        <div class="card-body p-2 text-left">
                            <h6><span class="badge badge-danger">Low</span></h6>
                            <h6 class="card-title mb-0"><?= $v['txt'] ?></h6>
                            <p class="card-text"><small style="line-height: 90%"><?= $v['desc'] ?></small></p>
                            <h5 class="text-danger mb-0"><?= number_format($v['price']) ?></h5>
                            <p class="small"><small class="text-secondary">
                                    <del>60</del>
                                </small> -10%
                            </p>
                            <a href="#" class="btn mt-2 btn-block btn-primary">Check</a>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach;  ?>
        </div>
    </div>
</div>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-18 19:41:24

您可以使用css's column-count属性,查看以下示例,根据需要进行调整。

代码语言:javascript
复制
.card-container {
  column-count: 2;
}
.card {
  margin-bottom: 1rem;
  break-inside: avoid-column;
}
代码语言:javascript
复制
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container bg-grey pt-4 pb-4">
  <h4 class="text-secondary">Buy</h4>
  <div class="row">
      <div class="col-12 col-lg-8">
          <div class="card-container">
        
        <div class="card">
          <img class="card-img-top" src="https://via.placeholder.com/150" alt="Card image cap">
          <div class="card-body">
            <h5 class="card-title">Card title 1</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
        
        
        <div class="card" >
          <img class="card-img-top" src="https://via.placeholder.com/150" alt="Card image cap">
          <div class="card-body">
            <h5 class="card-title">Card title 2</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
        
                
        <div class="card">
          <img class="card-img-top" src="https://via.placeholder.com/150" alt="Card image cap">
          <div class="card-body">
          <h5 class="card-title">Card title 3</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
        
        
        <div class="card">
          <img class="card-img-top" src="https://via.placeholder.com/150" alt="Card image cap">
          <div class="card-body">
          <h5 class="card-title">Card title 4 </h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
        
        <div class="card">
          <img class="card-img-top" src="https://via.placeholder.com/150" alt="Card image cap">
          <div class="card-body">
          <h5 class="card-title">Card title 5</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
        
        
        
         </div>
      </div>
  </div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2019-12-18 19:28:38

如果$product是0索引数组,则:

代码语言:javascript
复制
for ($index = 0; $index < count($product) / 2; $index++) {
    //Display a row, your left item is $product[$index]
    //and your right item is $product($index + ((int)count($product)) / 2)
    //the right item is missing if $index + ((int)count($product)) / 2 >= count($product)
    //So, if the comparison above is true, then you need to have a missing item
    //otherwise display the item having the given index
}

如果$product不是0索引数组,则可以将其转换为1:

代码语言:javascript
复制
$indexedProduct = [];
foreach($product as $k => $v) {
    $indexedProduct[]=$v;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59389625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档