dravpuso44681 2011-06-09 14:20
浏览 60
已采纳

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

Ok, right now I trying to get a list to echo out from mysql using a while loop, but as you can see here http://institute4se.com/plan/ it's echoing in the wrong order. Why?

The reason for the if statements is to make the top and bottom pieces of the div echo only when the loop reaches the beginning of a new subsection.

If I remove the if statement the list echos out in order but then the beginning and end of the div echo every loop.

You can see my database aka $info here: http://i.stack.imgur.com/QMO2X.png http://i.stack.imgur.com/RsHdb.png

Any thoughts on how to fix it?

 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条回答 默认 最新

  • dongqian1028 2011-06-15 16:09
    关注

    Here is the fixed code for anyone who is interested:

    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>
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?