【问题标题】:If-statement while loop stopping after first rowif 语句 while 循环在第一行后停止
【发布时间】:2014-07-13 09:31:19
【问题描述】:

我有以下 PHP 脚本,它从用户那里获取选定的 $empfullname 并根据选择打印“timecard_html”。我试图让它能够,如果他们选择“全部”,那么脚本会在单独的 timecard_html 中打印出列表中的所有员工。这是php脚本:

if ($empfullname == 'All') {
    $query = "select empfullname from ".$db_prefix."employees";
    $result = mysql_query($query);
    while ($row=mysql_fetch_array($result)) {
        $empfullname = ("".$row['empfullname']."");
        print timecard_html($empfullname, $local_timestamp_in_week);
        unset($empfullname);
    }
} else {
    print timecard_html($empfullname, $local_timestamp_in_week);

}

另外,这里是timecard_html,以备不时之需:

function timecard_html($empfullname, $local_timestamp_in_week) {
    // Return html of employee's timecard.
    global $show_display_name, $one_week;

    // SQL search parameters for one work week.
    $begin_local_timestamp = work_week_begin($local_timestamp_in_week);
    $end_local_timestamp = $begin_local_timestamp + $one_week;

    // Define helper functions for printing timecard header, footer, and for printing every row.
    function print_header($tc) {
        // Print timecard html header.
        global $overtime_week_limit, $timecard_display_running_total;

        $overtime_col = $overtime_week_limit > 0 ? "\n    <th align=\"center\" class=\"ovt\" title=\"Overtime hours\">OT</th>" : '';
        $total_col = $timecard_display_running_total == "yes" ? "\n    <th align=\"center\" class=\"total\" title=\"Running total of regular work hours and overtime to date.\">Total</th>" : '';
        print <<<End_Of_HTML

<table class="misc_items timecard_list" border="0" cellpadding="2" cellspacing="0" style="margin:0 auto;">
  <thead>
  <tr>
    <th align="left">In/Out</th>
    <th align="center">Time</th>
    <th align="center">Date</th>
    <th align="center" class="hrs" title="Regular work hours.">Hrs</th>$overtime_col$total_col
    <th align="left" class="notes">Notes</th>
  </tr>
  </thead>
  <tbody>
End_Of_HTML;
    }

    function print_row($tc) {
        // Configuration variables.
        global $timefmt, $datefmt;
        global $overtime_week_limit, $timecard_list_punch_outs, $timecard_display_hours_minutes;
        global $timecard_hours_include_overtime, $timecard_display_running_total;
        static $print_count = 0;

        if (($tc->in_or_out == 1) || $timecard_list_punch_outs == 'yes') {
            $h_color  = htmlentities($tc->row['color']);
            $h_inout  = htmlentities($tc->row['inout']);

            $h_time = date($timefmt, $tc->start_time);
            $h_date = date($datefmt, $tc->start_time);

            if ($timecard_display_hours_minutes == "yes") {
                $h_hours = hrs_min((($timecard_hours_include_overtime == "yes") ? ($tc->hours + $tc->overtime) : $tc->hours));
                $h_overtime = hrs_min($tc->overtime);
                $h_total = hrs_min(($tc->week_hours + $tc->overtime_hours));
            }
            else {
                $h_hours = sprintf("%01.02f",(($timecard_hours_include_overtime == "yes") ? ($tc->hours + $tc->overtime) : $tc->hours));
                $h_overtime = sprintf("%01.02f",$tc->overtime);
                $h_total = sprintf("%01.02f",($tc->week_hours + $tc->overtime_hours));
            }

            $h_notes  = htmlentities($tc->row['notes']);

            if ($tc->in_or_out != 1) {
                // Don't display hours on "out" records.
                $h_hours = $h_overtime = $h_total = '';
            }

            $row_class = (++$print_count % 2) ? 'odd' : 'even';
            $overtime_col = $overtime_week_limit > 0 ? "\n    <td align=\"right\" class=\"ovt\">$h_overtime</td>" : '';
            $total_col = $timecard_display_running_total == "yes" ? "\n    <td align=\"right\" class=\"total\">$h_total</td>" : '';
            print <<<End_Of_HTML

  <tr class="display_row $row_class">
    <td align="left" class="job_code" style="color:$h_color">$h_inout</td>
    <td align="right">$h_time</td>
    <td align="right">$h_date</td>
    <td align="right" class="hrs">$h_hours</td>$overtime_col$total_col
    <td align="left" class="notes">$h_notes</td>
  </tr>
End_Of_HTML;
        }
    }

    function print_footer($tc) {
        global $timecard_display_running_total, $timecard_hours_include_overtime;
        global $timecard_display_hours_minutes, $overtime_week_limit;

        // Set flag to print paragraph of totals if they're not already obvious.
        $print_totals = ($timecard_display_running_total == "yes" || $timecard_hours_include_overtime != "yes") ? true : false;

        $h_total_hours = sprintf("%01.02f",($tc->week_hours+$tc->overtime_hours));
        $h_totals = ($print_totals) ? "\n<p>Total for week: ".hrs_min($tc->week_hours + $tc->overtime_hours)." ($h_total_hours hours)</p>" : '';

        $h_ovt_total_hours = sprintf("%01.02f",$tc->overtime_hours);
        $h_overtime_totals = ($print_totals && $tc->overtime_hours > 0) ? "\n<p>Total overtime: ".hrs_min($tc->overtime_hours)." ($h_ovt_total_hours hours)</p>" : '';

        $h_day_total_hours = sprintf("%01.02f",$tc->today_hours);
        $h_today_hours = ($tc->today_hours !== null) ? "<p>Total today: ".hrs_min($tc->today_hours)." ($h_day_total_hours hours)</p>" : '';

        if ($timecard_display_running_total != "yes") {
            // Print row of totals 
            $total_hours = $timecard_hours_include_overtime == "yes" ? ($tc->week_hours+$tc->overtime_hours) : $tc->week_hours;
            $h_hours = $timecard_display_hours_minutes == "yes" ? hrs_min($total_hours) : $h_total_hours;
            $overtime_col = $overtime_week_limit > 0 ? "\n    <td align=\"right\" class=\"ovt\">".($timecard_display_hours_minutes == "yes" ? hrs_min($tc->overtime_hours) : $h_ovt_total_hours)."</td>" : '';
            $total_col = $timecard_display_running_total == "yes" ? "\n    <td align=\"right\" class=\"total\">".($timecard_display_hours_minutes == "yes" ? hrs_min($tc->week_hours+$tc->overtime_hours) : $h_total_hours)."</td>" : '';
            print <<<End_Of_HTML
  <tr class="total_row">
    <td align="left"></td>
    <td align="right"></td>
    <td align="right"></td>
    <td align="right" class="hrs">$h_hours</td>$overtime_col$total_col
    <td align="left" class="notes"></td>
  </tr>
End_Of_HTML;
        }

        print <<<End_Of_HTML
  </tbody>
</table>
End_Of_HTML;
        if ($timecard_display_running_total == "yes" || $timecard_hours_include_overtime != "yes" || $h_today_hours) {
            // Add totals text if totals are not already displayed or if summing the hours column is confusing.
            print <<<End_Of_HTML

<div class="totals">
$h_today_hours$h_totals$h_overtime_totals
</div>

End_Of_HTML;
        }
    }
    // End of helper function definitions.

    // Print timecard page header.
    $h_name_header = htmlentities( ($show_display_name == 'yes' ? get_employee_name($empfullname) : $empfullname) );
    $begin_date = date('l F j, Y',$begin_local_timestamp);
    print <<<End_Of_HTML

<div id="punchclock" class="timecard">
<h2>Timecard</h2>
<h3>$h_name_header</h3>
<h4>Week beginning $begin_date</h4>
End_Of_HTML;

    // Print timecard.
    $tc = new Timecard($empfullname,$begin_local_timestamp, $end_local_timestamp);
    list($row_count, $total_hours, $overtime_hours, $today_hours) = $tc->walk(print_header, print_row, print_footer);
    if ($row_count <= 0) print error_msg("No records were found.");

    // Print timecard page footer.
    print <<<End_Of_HTML
</div> <!-- timecard -->

End_Of_HTML;
}
?>

现在,如果选择“全部”,它会打印表中第一个员工的结果,但不会打印任何其他员工的结果。另外,如果我只打印("".$row['empfullname'].""),它会打印出所有员工。我是否可能需要使用 for each 循环而不是 while 循环?

提前致谢。

【问题讨论】:

  • 很难遵循您的代码,但我猜您的 HTML 嵌套存在问题。如果您查看源代码,您会看到其他员工的表格吗?
  • 顺便说一句,你为什么写$empfullname = ("".$row['empfullname'].""); 而不是简单的$empfullname = $row['empfullname'];?串联有什么用?
  • 不相关但很重要:这段代码的结构真的很糟糕。嵌套函数(为什么?唯一的区别是可读性更差)、全局变量、HTML 与业务逻辑混合、HTML 已过时……您可能需要改进它。
  • 我有似曾相识的感觉。几乎相同的问题:完全相同的问题,但发布的代码部分不同:stackoverflow.com/questions/23838736/…。有关系吗?
  • 这是一些老式的 HTML - "align" 属性早已被弃用。使用 CSS 更改布局。它也是一些老式的 PHP - 不再支持 mysql_ 函数。请改用mysqli_ 函数。不要忘记使用准备好的语句来保护自己免受 MYSQL 注入攻击。

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


【解决方案1】:

您的问题是您在函数中定义函数。这将在您第一次调用函数时起作用,但第二次将导致致命错误,因为该函数已定义。

查看simple example here。

将内部函数声明移出主函数应该可以解决这个特定问题。

您还应该在开发时始终启用错误处理和显示。在这种情况下,您会立即发现问题。

只需将其放在主脚本的顶部即可:

ini_set('display_errors',1);
error_reporting(E_ALL | E_STRICT);

【讨论】:

  • 感谢您的建议。我更新了我的功能,现在可以正常工作了。
猜你喜欢
  • 1970-01-01
  • 2014-07-01
  • 2022-09-29
  • 2021-04-25
  • 1970-01-01
  • 2023-01-06
  • 2015-05-19
  • 2015-10-20
  • 2019-01-11
相关资源
最近更新 更多