使用php在bootsrap中动态形成列

时间:2017-01-13 09:11:46

标签: php mysql twitter-bootstrap

我想在3个bootstrap colums中动态显示内容。

<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
<content> 
</div> <---- Problem (should be placed at the very end of the column, 
but the same code should also fill 2nd and 3rd columns)

我将查询提取到mysql。此外,我希望对象从左到右按降序排序(id = 51,id = 50,id = 42(对象标题中的最后一位数字是id)) 我能够做到这一点,使用row_number和row_number%3 = 0 row_number%3 = 1和row_number%3 = 2来显示3列,但我必须对数据库进行3次查询以及3个html块(这是坏)。

SELECT *
FROM (
    SELECT 
    @row_number:=@row_number + 1 AS rownum, id,title, description, ...etc
    FROM (
        SELECT @row_number:=0) r, flats ORDER BY id DESC) ranked WHERE rownum%3=1 ORDER BY id DESC

但是,当我开始实现过滤器时,它会导致显示对象出现问题,因为并非每列都有我想要显示的对象,但我对每一列都有自己的查询。 Problems with filters

我想要的:动态地形成3个单独的列,其内容使用一个MYSQL查询。 等待你的帮助! 3列中的1列示例:

<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
<?php
  $zapros = $DB_con->prepare('SELECT *
FROM (
    SELECT 
    @row_number:=@row_number + 1 AS rownum, id,title, description,type, area,address, square, name, phone, email, price, avito, farpost
    FROM (
        SELECT @row_number:=0) r, flats ORDER BY id DESC) ranked WHERE rownum%3=1 ORDER BY id DESC');
  $zapros->execute();
  $rowsone=$zapros->rowCount();
if($zapros->rowCount() > 0)
  {$tmpCount = 1;

    $count=$zapros->rowCount();
    while($data=$zapros->fetch(PDO::FETCH_ASSOC))
    {
      extract($data);


       $number=$data['id'];
                                              $imagestm=$DB_con->prepare('SELECT file FROM flats_images WHERE post_id=:uid LIMIT 1');
                                              $imagestm->bindParam(':uid',$number);
                                              $imagestm->execute();
                                              $imgrow=$imagestm->fetch(PDO::FETCH_ASSOC);
                                              extract($imgrow);?>

<div class="well-lg well-smallborders">
<a href="flat.php?flat_id=<?php echo $data['id']; ?>">
<img src="/mysql/uploads/<?php echo $imgrow['file']; ?>" class="img-responsive title-image center-block" /></a> 
<div class="flat-price center-block"><?php echo $data['price']; ?><span class="glyphicon glyphicon-rub" aria-hidden="true"></span> </div>
<div class="flat-title"><?php echo $data['title'];?> <?php echo $data['id']; ?></div>
<div class="dimmed-text left"><?php echo $data['type']; ?> </div>
<div class="dimmed-text right"><div class="tooltip_my"><span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span><?php echo $data['area'];?> <span class="tooltiptext"><?php echo $data['address']; ?></span></div></div>
<br>
<div class="dimmed-text left"><div class="tooltip_my"> <span class="glyphicon glyphicon-phone" aria-hidden="true"></span><?php echo $data['phone'];?><span class="tooltiptext"><?php echo $data['name'];?></span></div></div>
<div class="dimmed-text right"> <?php echo $data['square'];?></div>
<table class="flats-table">
 <tr>
 <td colspan="3">
    <button id="collapsar" class="btn btn-info pull-left" data-toggle="collapse" data-target="#hide-me<?php echo $tmpCount;?>"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> <span class="btntext">Показать миниатюры</span></button>
    <a href="flat.php?flat_id=<?php echo $data['id']; ?>" class="btn btn-info pull-left" ><span class="glyphicon glyphicon-eye-open"></span> <span class="btntext">Подробнее ...</span></a>
    </td>
    </tr>
    <tr>
     <td colspan="3">
 <div id="hide-me<?php echo $tmpCount;?>" class="collapse"> 
<? 
$imagestm2=$DB_con->prepare('SELECT file FROM flats_images WHERE post_id=:uid');
                                              $imagestm2->bindParam(':uid',$number);
                                              $imagestm2->execute();


for ($i = 1; $i <= $imagestm2->rowCount(); $i++) {
  $imgrow2=$imagestm2->fetch(PDO::FETCH_ASSOC);
                                              extract($imgrow2); ?>

<div class="col-xs-6 col-sm-6 col-md-4 col-lg-4">
<div class="thumbnail"> <img src="mysql\uploads\<?php echo $imgrow2['file']; ?>" data-toggle="modal" data-target="#myModal"  class="img-responsive"/></div>
</div>
    <?

} ?>
</div>
     </td>
     </tr>
</table>
</div><?
$tmpCount++;
      }}    
     ?>
     </div>

我想要的实际结果:Actual result

2 个答案:

答案 0 :(得分:0)

我认为你只是试图迭代你的结果,生成3列输出,然后也可以通过Javascript来应用过滤器,这可能会隐藏你的一些元素。我知道这样做的最简单方法如下:

<div class="row">
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
    ... 
    <!-- Bootstrap can manage with only 1 .row -->
</div>

答案 1 :(得分:0)

我不认为这是可能的,我想做什么,而是我找到了更简单的方式来显示内容:只需将3列合并为一个并将所有数据放在那里。是的,当你打开扰流板时它有问题,但我想我会处理它。谢谢你的所有答案。