繁体   English   中英

While循环并重用fetch_assoc()

[英]While loop and reusing fetch_assoc()

对于一个项目,我正在构建一个Flash游戏网站,并且我想知道如何重用这段代码:

        $result = $conn->query("SELECT DISTINCT `category` FROM beoordeling");

                <?php
                echo "<div class='row list jumbotron'>";
                while($data = $result->fetch_assoc()) {

                    echo "<div class='col-md-3'><a href='category.php?id=" . $data['category'] . "' class='thumbnail'><h4>" . ucfirst($data['category']) . " games</h4>";
                    echo "<img src='" . $imgLocation . $data['category'].".jpg' class='img-rounded' alt='" . $data['category'] . "' width='304' heigth='182'>";
                    echo "</a></div>";
                }
                echo "</div>";
            ?>      

我需要能够再次使用$data['category']动态填充所有游戏类别的菜单。 如果我只是尝试再次使用while循环,那么只有一个会起作用。 另一个留空。 非常感谢!

您需要调整结果指针以指向结果集的开头,以便可以再次使用它。 mysqli_result::data_seek()请使用mysqli_result::data_seek()方法。

这是参考:

因此,您的代码应如下所示:

<?php

    $result = $conn->query("SELECT DISTINCT `category` FROM beoordeling");

    echo "<div class='row list jumbotron'>";
    while($data = $result->fetch_assoc()) {

        echo "<div class='col-md-3'><a href='category.php?id=" . $data['category'] . "' class='thumbnail'><h4>" . ucfirst($data['category']) . " games</h4>";
        echo "<img src='" . $imgLocation . $data['category'].".jpg' class='img-rounded' alt='" . $data['category'] . "' width='304' heigth='182'>";
        echo "</a></div>";
    }
    echo "</div>";

    // adjust the result pointer to point to the beginning of the result set
    $result->data_seek(0);

    // now you can use the result set again
    // for example, you can iterate through it using while loop again

?>   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM