【问题标题】:While loop only displaying one rowWhile 循环只显示一行
【发布时间】:2014-07-13 08:59:21
【问题描述】:

我有一个 HTML 表单,允许用户从员工列表中进行选择。变量是“$empfullname”。但是,我还希望他们能够选择“全部”选项,如果选择,它应该让第一个员工运行 PHP 脚本,然后再选择下一个,依此类推。但是,如果选择“全部”,我现在拥有的代码只会显示表中的第一个员工并停止。也许我也需要他们的foreach?任何帮助是极大的赞赏。谢谢。

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

}

【问题讨论】:

  • timecard_html() 是否退出?
  • @dale 不,timecard_html 不退出()。好主意。必须检查:)
  • 试试这个查询:"select count(*) from ".$db_prefix."employees" 看看有多少条记录被返回。
  • 您应该将循环放在if 语句中,以避免出现有关$result 未定义等警告。

标签: php html if-statement foreach while-loop


【解决方案1】:

您的 while 循环不在 if 语句中,请尝试:

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

另外,我不会使用与员工全名相同的变量来检查是否选择了“全部”;)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 2011-11-02
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 2012-02-22
    相关资源
    最近更新 更多