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条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题