While循环仅在首次运行时有效

While循环仅在首次运行时有效

foreach打印出每个标题,然后我得到了所有的手风琴,但是除了第一个手风琴之外,所有的都是空的。我想念什么?

$result2 = mysqli_query($con, "SELECT * FROM sections ORDER BY `order`");
    $sectionnames = array();
    while($row = mysqli_fetch_array($result2)) {
        $sectionnames[] = $row['sectionname'];
    }

    $result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");

    foreach ($sectionnames as $sectionname) {
            echo '<h3 id="sectionname">' . $sectionname . '</h3>';
            echo '<div id="accordion">';
            while($row = mysqli_fetch_array($result)) {
                if ($sectionname == $row['section']) {
                    echo '<h3>' . $row['heading'] . '</h3>';
                    echo '<div>' . $row['content'] . '</div>';
                }
           }
           echo '</div>';
    }

最佳答案

没有您的架构,我无法确定,但是看起来常见问题与每个节的名称有关。如果是这样,则类似以下内容:

foreach ($sectionnames as $sectionname) {
        echo '<h3 id="sectionname">' . $sectionname . '</h3>';
        echo '<div id="accordion">';
        $result = mysqli_query($con,"SELECT * FROM faq where section = '$sectionname' ORDER BY `order`");
        while($row = mysqli_fetch_array($result)) {
           echo '<h3>' . $row['heading'] . '</h3>';
           echo '<div>' . $row['content'] . '</div>';
       }
       echo '</div>';
}

关于php - While循环仅在首次运行时有效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17376508/

10-09 00:58