dongshi4078 2015-09-05 11:58
浏览 40

如何在MySQL和PHP的while循环中返回不同html行中的不同html列?

My trouble is probably easy to solve for you, but despite attempts, I was unable to solve. I would like to return all database values subdiveded per row and column. I'll explain easier: I would like to enter a maximum of four columns and as many rows as records divided by columns. Example:

<div class="row categorie" style="margin:0; padding: 0;">
    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 no-padding">
        <div class="categoria"><a href="offerte.php?s=1"><img src="img/slide1.jpg" class="img-responsive center-block"></a>
        <p class="nome">Cucine</p></div>
    </div>

    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 no-padding">
        <div class="categoria"><a href="offerte.php?s=2"><img src="img/slide2.jpg" class="img-responsive center-block"></a>
        <p class="nome">Soggiorni</p></div>
    </div>

    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 no-padding">
        <div class="categoria"><a href="offerte.php?s=3"><img src="img/slide3.jpg" class="img-responsive center-block"></a>
        <p class="nome">Divani</p></div>
    </div>

    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 no-padding">
        <div class="categoria"><a href="offerte.php?s=4"><img src="img/slide4.jpg" class="img-responsive center-block"></a>
        <p class="nome">Bagni</p></div>
    </div>
</div>
<div class="row categorie" style="margin:0; padding: 0;">
    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 no-padding">
        <div class="categoria"><a href="offerte.php?s=5"><img src="img/slide5.jpg" class="img-responsive center-block"></a>
        <p class="nome">Tavoli e sedie</p></div>
    </div>

    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 no-padding">
        <div class="categoria"><a href="offerte.php?s=6"><img src="img/slide6.jpg" class="img-responsive center-block"></a>
        <p class="nome">Camere</p></div>
    </div>

    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 no-padding">
        <div class="categoria"><a href="offerte.php?s=7"><img src="img/slide7.jpg" class="img-responsive center-block"></a>
        <p class="nome">Accessori</p></div>
    </div>
</div>

This is what I would like to obtain. But the code returns a single row instead of two. This is the code i wrote:

<div class="row categorie" style="margin:0; padding: 0;">
<?php $query = "SELECT * FROM categorie";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){ 
?>
    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 no-padding">
        <div class="categoria"><a href="offerte.php?s=<?= $row[0]; ?>"><img src="img/slide<?= $row[0]; ?>.jpg" class="img-responsive center-block" /></a>
        <p class="nome"><?= $row[1]; ?></p></div>
    </div>
<?php } ?>
</div>
  • 写回答

1条回答 默认 最新

  • doulou1989 2015-09-05 12:10
    关注

    Try this:

       $results = array();
        while($row = mysql_fetch.........)
              $results[] = $row;
    
    
     foreach(array_chunk($results, 4)
            as $row)){
        echo '<div class = "row-with-4-items">';   
    
       foreach($row as $item)
             print_r($item);
    
        echo '</div>';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用