dpb35161 2016-04-24 02:21
浏览 50
已采纳

PHP For循环不产生输出

simple php for loop, worked fine before the loop was added, but I want it to print 4 rows of 3 products. Problem is its putting all 12 items into a single row! Then it creates 3 more empty rows at the bottom of the page.

Added updated version of the code, figured it might help someone else, although it displays correctly I have to pull 15 records from the table.

<?php for($i = 0; $i < 4; $i++) { ?>
            <div class="row products">
                <?php while($product = mysqli_fetch_assoc($featured)) : ?>

                <!-- product-->
                <div class="col-md-4 col-sm-6">
                    <div class="product">
                        <div class="image">
                            <a href="#"><img src=" <?= "images/wheels/wheelphotos/". $product["bigpic"]; ?>" alt= "<?= $product["manufacturer"]; ?>" class="img-responsive"></a>
                            <div class="quick-view-button"><button type="button" onclick="quickModal(<?= $product["recid"]; ?>)"  class="btn btn-default btn-sm">Quick view</button></div>
                        </div>
                        <div class="text">
                            <p class="brand"><a href="#"><?= $product["manufacturer"]; ?></a></p>
                            <h3> <a href="detail.html"><?= $product["diam"]; ?></a></h3>
                            <p class="price">$<?= $product["rrp"]; ?></p>
                            <button class="btn btn-default btn-sm btn-primary">Details</button>
                        </div>
                    </div>
                </div>

                <?php endwhile; ?>
            </div>
            <?php } ?>


<?php for($i = 0; $i < 4; $i++) { ?>
            <div class="row products">
                <?php
                $j = 0;
                while($product = mysqli_fetch_assoc($featured)) :
                $j++;
                    if($j > 3 ){
                        break;
                    }
                ?>
                <!-- product-->
                <div class="col-md-4 col-sm-6">
                    <div class="product">
                        <div class="image">
                            <a href="#"><img src=" <?= "images/wheels/wheelphotos/". $product["bigpic"]; ?>" alt= "<?= $product["manufacturer"]; ?>" class="img-responsive"></a>
                            <div class="quick-view-button"><button type="button" onclick="quickModal(<?= $product["recid"]; ?>)"  class="btn btn-default btn-sm">Quick view</button></div>
                        </div>
                        <div class="text">
                            <p class="brand"><a href="#"><?= $product["manufacturer"]; ?></a></p>
                            <h3> <a href="detail.php"><?= $product["diam"]; ?></a></h3>
                            <p class="price">$<?= $product["rrp"]; ?></p>
                            <button class="btn btn-default btn-sm btn-primary" href="detail.php">Details</button>
                        </div>
                    </div>
                </div>

                <?php endwhile; ?>
            </div>
            <?php } ?>
  • 写回答

1条回答 默认 最新

  • dtlrp119999 2016-04-24 02:35
    关注

    Your while loop is going to run for as many records as your query selected. If you need to get out of that loop after a certain number of records, you can use a variable to count how many times you've been through the loop, then break out of the while loop. Your for loop will go through its next iteration and print the "row" div, and the while loop will process the next couple of records until you break again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决