While 循环仅适用于第一次运行 [英] While Loop Only Works On First Run

查看:31
本文介绍了While 循环仅适用于第一次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach 打印出每个标题,我得到了所有的手风琴,但是除了第一个手风琴之外的所有东西都是空的.我错过了什么?

$result2 = mysqli_query($con, "SELECT * FROM Section 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 '

'.$sectionname .'</h3>';echo '<div id="accordion">';while($row = mysqli_fetch_array($result)) {if ($sectionname == $row['section']) {回声'<h3>'.$row['heading'] .'</h3>';echo '

'.$row['content'] .'</div>';}}回声'</div>';}

解决方案

如果没有您的架构,我无法确定,但看起来 faq 与部分名称相关.如果这是真的,就像这样:

foreach ($sectionnames as $sectionname) {echo '

'.$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)) {回声'<h3>'.$row['heading'] .'</h3>';echo '

'.$row['content'] .'</div>';}回声'</div>';}

The foreach prints out each of the headers, and I get all the accordions, but everything but the first accordion are empty. What am I missing?

$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>';
    }

解决方案

Without your schema I can't be sure, but it looks like faq is related to section by sectionname. If that's true, something like this:

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>';
}

这篇关于While 循环仅适用于第一次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆