简体   繁体   English

基于mysql查询计数引导多行

[英]Bootstrap multiple rows based on mysql query count

I am using Bootstrap to create a row of 6 colunm images. 我正在使用Bootstrap创建6张colunm图像的行。

<div class="row">
    <?php
    $sql = //my query
    while($row = mysqli_fetch_array($sql)){
    ?>
    <div class="col-xs-6 col-md-2">
       <img id="img" class="img-responsive" src="img/<?php echo $row['image']; ?>" />
    </div>
    <?php
    }
    ?>
</div>

Which works fine when there is only 6 rows from the database, however when there is 7 the extra one gets bumped to the far right rather than the far left on the next row, see example below. 当数据库中只有6行时,这很好用,但是当有7行时,多余的行将碰到最右边而不是最下一行的最左边,请参见下面的示例。

| img | img | img | img | img | img
                              | img

Normal result i am looking for would be 我正在寻找的正常结果是

| img | img | img | img | img | img
| img

I want to create a new bootstrap row after every 6 results, any help would be appriciated 我想在每6个结果后创建一个新的引导行,任何帮助都将适用

You could fetch all the values into an array first, count how many elements are in the array and then dynamically control what span classes you use. 您可以先将所有值提取到数组中,计算数组中有多少个元素,然后动态控制要使用的span类。

<div class="row">
<?php
$sql = //my query
$images = array();
while($row = mysqli_fetch_assoc($sql)){
    $images[] = $row['image'];
}
$column_class = count($images) > 6 ?  "col-xs-3 col-md-1" : "col-xs-6 col-md-2";

foreach ($images as $image) {
?>
<div class="<?= $column_class ?>">
   <img id="img" class="img-responsive" src="img/<?= $image ?>" />
</div>
<?php
}
?>

i have seen this before when the heights of the images are not identical. 当图像的高度不同时,我已经看到了这一点。 the float left only floats to the point where the bottom line is uneven. 左浮动仅浮动到底线不均匀的点。 try adding a style height with a reasonable fixed height. 尝试添加具有合理固定高度的样式高度。

<div class="row">
    <?php
    $sql = //my query
    while($row = mysqli_fetch_array($sql)){
    ?>
    <div class="col-xs-6 col-md-2">
       <img id="img" class="img-responsive" style="height:100px" src="img/<?php echo $row['image']; ?>" />
    </div>
    <?php
    }
    ?>
</div>

I ran into this problem in a recent project and was able to solve it with this code. 我在最近的一个项目中遇到了这个问题,并且能够使用此代码解决它。 I only have two views so I only need the two lines but you could have more if you wanted. 我只有两个视图,所以我只需要两行,但是如果需要,您可以有更多视图。 You can see this in action on this page 您可以在此页面上看到此操作

<div class="visible-md-block visible-lg-block clearfix">

<div class="visible-sm-block visible-xs-block clearfix">

Here is the full php code for the gallery page 这是图库页面的完整php代码

<?php
    $c = 1;
    foreach($images as $image) {?>
     <div class="col-xs-4 col-md-3">
       <a class="thumbnail" href="path to full size image" data-toggle="lightbox" data-footer="<?php echo $image['caption']; ?>">
        <img class="img-responsive" src="path to thumbnail" alt="...">
      </a>
     </div>
<?php
        if(($c % 4) == 0) {
            echo '<div class="visible-md-block visible-lg-block clearfix"></div>';
        }

        if(($c % 3) == 0) {
            echo '<div class="visible-sm-block visible-xs-block clearfix"></div>';
        }

        $c++;
    }
?>

Sorry while this code works for when you want the number of images in a row to change at different screen sizes I just re read your question and you say you just want 6 across all the time so you could just use one line 抱歉,此代码适用于当您希望在不同屏幕尺寸下连续更改图像数量时,我只是重新阅读了您的问题,并且您说您一直想要6张图像,所以您只能使用一行

if(($c % 6) == 0) {
            echo '<div class="clearfix"></div>';
        }

The $c variable is just a counter and the code $c % 6 == 0 will put the clearfix div every time $c is divisible by 6. $ c变量只是一个计数器,每次$ c被6整除时,代码$ c%6 == 0都会放入clearfix div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM