【问题标题】:PHP For loop not producing outputPHP For循环不产生输出
【发布时间】:2016-04-24 02:21:52
【问题描述】:

简单的 php for 循环,在添加循环之前运行良好,但我希望它打印 4 行,每行 3 个产品。问题是它将所有 12 个项目放在一行中!然后它在页面底部再创建 3 个空行。

添加了代码的更新版本,认为它可能对其他人有所帮助,尽管它显示正确我必须从表中提取 15 条记录。

<?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 } ?>

【问题讨论】:

    标签: php for-loop while-loop


    【解决方案1】:

    您的while 循环将针对您选择的查询运行尽可能多的记录。如果您需要在一定数量的记录后退出该循环,您可以使用一个变量来计算您已经通过了多少次循环,然后 break 退出 while 循环。您的for 循环将进行下一次迭代并打印“行” div,while 循环将处理接下来的几条记录,直到您再次break

    【讨论】:

    • 您好,感谢您的回复,我的问题是我不习惯编写 php,并且正在努力将 php 逻辑与 html 输出集成,我该将 if 语句放在哪里?我刚刚尝试使用while循环添加它但没有成功,为什么每个人都反对这个问题,php作为一个整体在他们的网站上很少有将php与html集成的例子,只是直接的php语法。
    猜你喜欢
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多