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.

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

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面