While loop only displaying one row

隐身守侯 提交于 2019-12-25 19:15:10

问题


I have an HTML form that allows the user to select from a list of employees. The variable is '$empfullname.' However, I also want them to be able to select an 'All' option and if selected it should take the first employee and run the PHP script, then take the next, and so on and so forth. However, the code I have right now if 'All' is selected just displays the first employee in the table and stops. Maybe I need a foreach in their as well? Any help is greatly appreciated. Thanks.

if ($empfullname == 'All') {
    $query = "select * from ".$db_prefix."employees order by empfullname asc";
    $result = mysql_query($query);
} else {
    print timecard_html($empfullname, $local_timestamp_in_week);
}

while ($row=mysql_fetch_array($result)) {
    print timecard_html(stripslashes("".$row['empfullname'].""), $local_timestamp_in_week);

}

回答1:


Your while loop isn't within the if statement, try:

if ($empfullname == 'All') {
    $query = "select * from ".$db_prefix."employees order by empfullname asc";
    $result = mysql_query($query);

    while ($row=mysql_fetch_array($result)) {
         print timecard_html(stripslashes("".$row['empfullname'].""),    $local_timestamp_in_week);
    }
} 

else {
    print timecard_html($empfullname, $local_timestamp_in_week);
}

Also, I wouldn't use the same variable as the Employee's Full Name to check if 'All' is selected ;).



来源:https://stackoverflow.com/questions/23838736/while-loop-only-displaying-one-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!