PHP MYSQL while循环,if语句以错误的顺序回显

时间:2011-06-09 14:20:32

标签: php mysql loops if-statement while-loop

好的,现在我尝试使用while循环获取一个列表来回显mysql,但正如你在这里看到的那样http://institute4se.com/plan/它以错误的顺序回显。为什么?

if语句的原因是只在循环到达新子节的开头时才使div的顶部和底部回显。

如果我删除if语句,列表按顺序回显,但是div的开头和结尾都回显每个循环。

您可以在此处查看我的数据库又名$ info:http://i.stack.imgur.com/QMO2X.png http://i.stack.imgur.com/RsHdb.png

有关如何修复它的任何想法?

 while($info = mysql_fetch_array( $data )) 
 {

if($info['subsection']=='0'){
echo "
<div class='menuSection'>
<div class='sectionHeader' id='header".$info['section']."'><img src='images/status_circ_empty.gif' alt='Section Empty' /><a href='index.php?s=".$info['section']."&ss=".$info['subsection']."'>".$titleArr[$info['section']]."</a><img src='images/arrow_".$currentArrow.".gif' alt='Arrow' class='arrow' /></div>
    <ul class='sectionSubmenu' id='section".$info['section']."' style='display:".$currentSection."'>
"; 
}
else{
echo "<li><img src='images/status_circ_empty.gif' alt='Section Empty' /><a href='index.php?s=".$info['section']."&ss=".$info['subsection']."'>".$info['title']."</a></li>"; 
}
if($info['subsection']=='0'){
echo"
    </ul>
</div>
";
}
}

2 个答案:

答案 0 :(得分:0)

您需要订购记录,将ORDER BY subsection ASC添加到查询中。

答案 1 :(得分:0)

以下是感兴趣的人的固定代码:

while($info = mysql_fetch_array( $data )) 
{ 

    // if the current subsection is a new section
    if($info['subsection']==0){
    // if the current section is NOT the first section
    if($info['section']!=0){
    // end the section div
    echo "
            </ul>
    </div>
    ";
    }

    // start the section div and add the section header
    echo "
    <div class='menuSection'>
    <div class='sectionHeader' id='header".$info['section']."'><img src='images/status_circ_empty.gif' alt='Section Empty' /><a href='index.php?s=".$info['section']."&ss=".$info['subsection']."'>".$titleArr[$info['section']]."</a><img src='images/arrow_".$currentArrow.".gif' alt='Arrow' class='arrow' /></div>
        <ul class='sectionSubmenu' id='section".$info['section']."' style='display:".$currentSection."'>
        <li><img src='images/status_circ_empty.gif' alt='Section Empty' /><a href='index.php?s=".$info['section']."&ss=".$info['subsection']."'>".$info['title']."</a></li>

    ";

    }
    // if the current section is not a new section
    else{
        // add the next submenu item
        echo "<li><img src='images/status_circ_empty.gif' alt='Section Empty' /><a href='index.php?s=".$info['section']."&ss=".$info['subsection']."'>".$info['title']."</a></li>"; 
    }
} 
echo"
    </ul>
</div>
";