PHP – 运行多个 foreach 循环导致页面停止加载数据

huangapple go评论76阅读模式
英文:

PHP - Running multiple foreach loops causes page to stop loading data

问题

我正在创建一个个人项目,这是一个时间打卡应用程序。在这个页面上(参考截图),我正在使用foreach循环来调用工资周期列表(包括过去和现在的周期)和员工打卡数据。我还使用了一个foreach循环来检查员工的状态,看看他们是否已经打卡了上班、下班休息、从休息回来、午休、午休回来或下班。

之前,我将此页面专门用于查看“上班”按钮和实时时钟。但是,在意识到此页面上有多少空白空间后,我决定要显示员工的时间表<table>。然而,在重新加载页面后,实时时钟消失了,员工打卡数据也没有加载,下拉选择框下的支付周期列表也没有显示。

发生错误后,我试图思考是什么导致了页面停止。所以,我尝试注释掉了调用支付周期列表的foreach循环,员工时间表的数据就显示出来了。但现在,支付周期列表下的下拉选择框为空,没有数据。我还尝试注释掉加载员工打卡数据的foreach循环,是的,支付周期列表显示出来了。我还尝试注释掉检查员工状态的循环,现在支付周期列表和员工打卡数据都显示出来了。

使用foreach循环检查员工状态的背景:我使用它来确定员工可以进行的允许打卡操作(上班/下班按钮)。

我不想假设在页面中有多个foreach循环会导致应用程序出现这种情况,但我想知道,对于解决这种问题,会建议采取什么样的修复措施?是否认为有一种更好的方法可以获取所有这些数据,而无需使用多个foreach循环。

用于确定员工可以进行的允许打卡操作的Foreach循环

function buttonGroup($params)
{

    $params = filterParams($params);
    $params = getEmpStatus($params);

    foreach ($params['results'] as $row)
    {

        switch ($row['emp_status'])
        {

            case 'OUT':
                $params['buttonGroup'] = 
                '<button type="submit" name="in_day" class="px-4 py-2 rounded-md bg-green-500 text-white">In Day</button>';
            break;

            case 'IN':
                $params['buttonGroup'] = 
                '<button type="submit" name="out_day" class="px-4 py-2 rounded-md bg-red-700 text-white">Out Day</button>
                <button type="submit" name="out_break_1" class="px-4 py-2 rounded-md bg-yellow-500 text-white">Out Break</button>';
            break;

            case 'OUT_BREAK_1':
                $params['buttonGroup'] = 
                '<button type="submit" name="out_day" class="px-4 py-2 rounded-md bg-red-700 text-white">Out Day</button>
                <button type="submit" name="in_break_1" class="px-4 py-2 rounded-md bg-green-500 text-white">In Break</button>';
            break;
            
            case 'IN_BREAK_1':
                $params['buttonGroup'] = 
                '<button type="submit" name="out_day" class="px-4 py-2 rounded-md bg-red-700 text-white">Out Day</button>
                <button type="submit" name="out_lunch" class="px-4 py-2 rounded-md bg-blue-700 text-white">Out Lunch</button>';
            break;

            case 'OUT_LUNCH':
                $params['buttonGroup'] = 
                '<button type="submit" name="out_day" class="px-4 py-2 rounded-md bg-red-700 text-white">Out Day</button>
                <button type="submit" name="in_lunch" class="px-4 py-2 rounded-md bg-green-500 text-white">In Lunch</button>';
            break;

            case 'IN_LUNCH':
                $params['buttonGroup'] = 
                '<button type="submit" name="out_day" class="px-4 py-2 rounded-md bg-red-700 text-white">Out Day</button>
                <button type="submit" name="out_break_2" class="px-4 py-2 rounded-md bg-yellow-500 text-white">Out Break</button>';
            break;

            case 'OUT_BREAK_2':
                $params['buttonGroup'] = 
                '<button type="submit" name="out_day" class="px-4 py-2 rounded-md bg-red-700 text-white">Out Day</button>
                <button type="submit" name="in_break_2" class="px-4 py-2 rounded-md bg-green-500 text-white">In Break</button>';
            break;

            case 'IN_BREAK_2':
                $params['buttonGroup'] = 
                '<button type="submit" name="out_day" class="px-4 py-2 rounded-md bg-red-700 text-white">Out Day</button>';
            break;

        }

    }

    return $params;

}

用于获取支付周期列表的Foreach循环

function viewPayPeriods($params)
{

    $params = filterParams($params);
    $params['results'] = getPayPeriods($params);

    foreach ($params['results'] as $row)
    {

        $params['payPeriodsList'] =
        '<option value="'.$row['id'].'">'.date('m/d/Y', strtotime($row['pp_start'])).' - '.date('m/d/Y', strtotime($row['pp_end'])).'</option>';

        echo $params['payPeriodsList'];

    }

}

用于获取员工打卡数据的Foreach循环

function readOnlyTimesheet($params)
{

    $params = filterParams($params);
    $params['results'] = userPunchData($params);

    foreach ($params['results'] as $row)
    {

        $params['timesheetRow'] =
        '<tr>
            <td class="whitespace-nowrap px-4 py-2 font-medium text-gray-900 text-center">'.date('D m/d/Y', strtotime($row['punch_day'])).'</td>
            <td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date

<details>
<summary>英文:</summary>

I am creating a personal project which is a time punch application. On this page *(refer to [screenshot](https://i.stack.imgur.com/qIyxa.png))*, I am using foreach loop(s) to call in a list of pay periods *(both past &amp; present)* and employee punch data. I am also using a foreach loop to check the status of the employee if they have punched IN, OUT for break, IN from break, OUT for lunch, IN from lunch, or OUT for the day.

Previously, I had this page dedicated only to seeing the &quot;In Day&quot; button and a live clock. But, after realizing how much dead space I have on this page, I decided I wanted to display the employee timesheet `&lt;table&gt;` as well. However, after reloading the page, the live clock disappears and the employee punch data doesn&#39;t load, along with the list of pay-periods under the `&lt;select&gt;` element.

Once the error occurred, I tried to think what causes the page to stop. So, I tried to comment out the foreach loop that calls in a list of the pay periods &amp; *voila*, the employee punch data for the timesheet *displays*. But now, that leaves the list of pay periods under the `&lt;select&gt;` element, empty, with no data. I tried commenting out the foreach loops that load in the employee punch data and yes, the list of pay periods displays. I also tried commenting out the loop that checks the status of the employee, and now both the list of pay periods and employee punch data display.

The context for using a foreach loop to check the status of the employee: I use it to *determine* the allowed punches (IN/OUT buttons) the employee can make.

I didn&#39;t want to assume that having multiple foreach loops in a page would cause the app to break like this, but I guess since I came across this problem, *what* would you suggest as a fix that would solve an issue like this? *Would* you say there is a better way to get all of this data without having multiple foreach loops.

**Foreach Loop that determines allowed punches the employee can make**

function buttonGroup($params)
{

$params = filterParams($params);
$params = getEmpStatus($params);
foreach ($params[&#39;results&#39;] as $row)
{
switch ($row[&#39;emp_status&#39;])
{
case &#39;OUT&#39;:
$params[&#39;buttonGroup&#39;] = 
&#39;&lt;button type=&quot;submit&quot; name=&quot;in_day&quot; class=&quot;px-4 py-2 rounded-md bg-green-500 text-white&quot;&gt;In Day&lt;/button&gt;&#39;;
break;
case &#39;IN&#39;:
$params[&#39;buttonGroup&#39;] = 
&#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
&lt;button type=&quot;submit&quot; name=&quot;out_break_1&quot; class=&quot;px-4 py-2 rounded-md bg-yellow-500 text-white&quot;&gt;Out Break&lt;/button&gt;&#39;;
break;
case &#39;OUT_BREAK_1&#39;:
$params[&#39;buttonGroup&#39;] = 
&#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
&lt;button type=&quot;submit&quot; name=&quot;in_break_1&quot; class=&quot;px-4 py-2 rounded-md bg-green-500 text-white&quot;&gt;In Break&lt;/button&gt;&#39;;
break;
case &#39;IN_BREAK_1&#39;:
$params[&#39;buttonGroup&#39;] = 
&#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
&lt;button type=&quot;submit&quot; name=&quot;out_lunch&quot; class=&quot;px-4 py-2 rounded-md bg-blue-700 text-white&quot;&gt;Out Lunch&lt;/button&gt;&#39;;
break;
case &#39;OUT_LUNCH&#39;:
$params[&#39;buttonGroup&#39;] = 
&#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
&lt;button type=&quot;submit&quot; name=&quot;in_lunch&quot; class=&quot;px-4 py-2 rounded-md bg-green-500 text-white&quot;&gt;In Lunch&lt;/button&gt;&#39;;
break;
case &#39;IN_LUNCH&#39;:
$params[&#39;buttonGroup&#39;] = 
&#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
&lt;button type=&quot;submit&quot; name=&quot;out_break_2&quot; class=&quot;px-4 py-2 rounded-md bg-yellow-500 text-white&quot;&gt;Out Break&lt;/button&gt;&#39;;
break;
case &#39;OUT_BREAK_2&#39;:
$params[&#39;buttonGroup&#39;] = 
&#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
&lt;button type=&quot;submit&quot; name=&quot;in_break_2&quot; class=&quot;px-4 py-2 rounded-md bg-green-500 text-white&quot;&gt;In Break&lt;/button&gt;&#39;;
break;
case &#39;IN_BREAK_2&#39;:
$params[&#39;buttonGroup&#39;] = 
&#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;&#39;;
break;
}
}
return $params;

}


**Foreach Loop that pulls in list of pay periods**

function viewPayPeriods($params)
{

$params = filterParams($params);
$params[&#39;results&#39;] = getPayPeriods($params);
foreach ($params[&#39;results&#39;] as $row)
{
$params[&#39;payPeriodsList&#39;] =
&#39;&lt;option value=&quot;&#39;.$row[&#39;id&#39;].&#39;&quot;&gt;&#39;.date(&#39;m/d/Y&#39;, strtotime($row[&#39;pp_start&#39;])).&#39;&#39;.&#39; - &#39;.&#39;&#39;.date(&#39;m/d/Y&#39;, strtotime($row[&#39;pp_end&#39;])).&#39;&lt;/option&gt;&#39;;
echo $params[&#39;payPeriodsList&#39;];
}

}


**Foreach Loop that pulls in employee punch data**

function readOnlyTimesheet($params)
{

$params = filterParams($params);
$params[&#39;results&#39;] = userPunchData($params);
foreach ($params[&#39;results&#39;] as $row)
{
$params[&#39;timesheetRow&#39;] =
&#39;&lt;tr&gt;
&lt;td class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900 text-center&quot;&gt;&#39;.date(&#39;D m/d/Y&#39;, strtotime($row[&#39;punch_day&#39;])).&#39;&lt;/td&gt;
&lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;in_day&#39;])).&#39;&lt;/td&gt;
&lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;out_break_1&#39;])).&#39;&lt;/td&gt;
&lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;in_break_1&#39;])).&#39;&lt;/td&gt;
&lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;out_lunch&#39;])).&#39;&lt;/td&gt;
&lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;in_lunch&#39;])).&#39;&lt;/td&gt;
&lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;out_break_2&#39;])).&#39;&lt;/td&gt;
&lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;in_break_2&#39;])).&#39;&lt;/td&gt;
&lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;out_day&#39;])).&#39;&lt;/td&gt;
&lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.$row[&#39;total_hours&#39;].&#39;&lt;/td&gt;
&lt;/tr&gt;&#39;;
echo $params[&#39;timesheetRow&#39;];
}

}


**Home Page that loads in data as described above**
&lt;section class=&quot;px-4 py-6&quot;&gt;
&lt;div class=&quot;container mx-auto space-y-4&quot;&gt;
&lt;div class=&quot;flex flex-col items-center justify-center space-y-1&quot;&gt;
&lt;div id=&quot;time&quot; class=&quot;font-semibold text-3xl&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;date&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;flex items-center justify-center space-x-4&quot;&gt;
&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
&lt;?php 
$params = buttonGroup($params);
echo $params[&#39;buttonGroup&#39;];
?&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;px-4 py-6&quot;&gt;
&lt;div class=&quot;container mx-auto w-2/3 space-y-4&quot;&gt;
&lt;form action=&quot;&quot; method=&quot;post&quot; class=&quot;w-1/3 flex flex-row items-end space-x-4&quot;&gt;
&lt;div&gt;
&lt;label for=&quot;HeadlineAct&quot; class=&quot;block text-sm font-medium text-gray-900&quot;&gt;
Select Pay-Period:
&lt;/label&gt;
&lt;select name=&quot;pay_period&quot; id=&quot;HeadlineAct&quot; class=&quot;mt-1.5 p-2 w-full rounded-lg border-gray-300 text-gray-700 sm:text-sm&quot;&gt;
&lt;?php
$params = viewPayPeriods($params);
echo $params[&#39;payPeriodsList&#39;];
?&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;button type=&quot;submit&quot; name=&quot;pp_view&quot; class=&quot;rounded-lg bg-blue-600 px-5 py-2 font-medium text-white&quot;&gt;View&lt;/button&gt;
&lt;/form&gt;
&lt;div class=&quot;rounded-lg border border-gray-200 overflow-hidden&quot;&gt;
&lt;table class=&quot;w-full divide-y-2 divide-gray-200 bg-white text-sm&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
Date
&lt;/th&gt;
&lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
In Day
&lt;/th&gt;
&lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
Out Break
&lt;/th&gt;
&lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
In Break
&lt;/th&gt;
&lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
Out Lunch
&lt;/th&gt;
&lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
In Lunch
&lt;/th&gt;
&lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
Out Break
&lt;/th&gt;
&lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
In Break
&lt;/th&gt;
&lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
Out Day
&lt;/th&gt;
&lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
Total Hours
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody class=&quot;divide-y divide-gray-200&quot;&gt;
&lt;?php
$params = readOnlyTimesheet($params);
echo $params[&#39;timesheetRow&#39;];
?&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;flex flex-row items-center justify-end&quot;&gt;
&lt;span class=&quot;font-medium&quot;&gt;Total Hours worked: &lt;?php $params = viewTotalHours($params); echo $params[&#39;totalHours&#39;]; ?&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/section&gt;
</details>
# 答案1
**得分**: 1
感谢hakre和imvain2对我的问题提供的评论和一些建议。我通过阅读imvain2的答案成功解决了它。
为了修复问题,我做了以下操作:
- 由于我的functions()中有echo语句,我决定测试并注释掉我在index.php中运行的echo语句。
```html
<section class="px-4 py-6">
<div class="container mx-auto space-y-4">
<div class="flex flex-col items-center justify-center space-y-1">
<div id="time" class="font-semibold text-3xl"></div>
<div id="date"></div>
</div>
<div class="flex items-center justify-center space-x-4">
<form action="" method="post">
<?php 
$params = buttonGroup($params);
// echo $params['buttonGroup'];
?>
</form>
</div>
</div>
</section>
<section class="px-4 py-6">
<div class="container mx-auto w-2/3 space-y-4">
<form action="" method="post" class="w-1/3 flex flex-row items-end space-x-4">
<div>
<label for="HeadlineAct" class="block text-sm font-medium text-gray-900">
选择工资周期:
</label>
<select name="pay_period" id="HeadlineAct" class="mt-1.5 p-2 w-full rounded-lg border-gray-300 text-gray-700 sm:text-sm">
<?php
$params = viewPayPeriods($params);
// echo $params['payPeriodsList'];
?>
</select>
</div>
<button type="submit" name="pp_view" class="rounded-lg bg-blue-600 px-5 py-2 font-medium text-white">查看</button>
</form>
<div class="rounded-lg border border-gray-200 overflow-hidden">
<table class="w-full divide-y-2 divide-gray-200 bg-white text-sm">
<thead>
<tr>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
日期
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
上班时间
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
第一次休息
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
第一次休息结束
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
午餐时间
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
午餐结束
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
第二次休息
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
第二次休息结束
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
下班时间
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
总工时
</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
<?php
$params = readOnlyTimesheet($params);
// echo $params['timesheetRow'];
?>
</tbody>
</table>
</div>
<div class="flex flex-row items-center justify-end">
<span class="font-medium">总工时:<?php $params = viewTotalHours($params); echo $params['totalHours'];?></span>
</div>
</div>
</section>

imvain2提到,虽然我的函数(readOnlyTimesheet)中有echo语句,但我有return $params,并指出我没有返回任何内容。感谢你指出这一点。所以在尝试了一些事情后,比如注释掉我的index.php中的echo语句,只使用我在functions()中的内容,然后去掉我的return $params,我得到了[这个][1]。

function buttonGroup($params)
{

    $params = filterParams($params);
    $params = getEmpStatus($params);

    foreach ($params['results'] as $row)
    {

        switch ($row['emp_status'])
        {

            case 'OUT':
                $params['buttonGroup'] = 
                '<button type="submit" name="in_day" class="px-4 py-2 rounded-md bg-green-500 text-white">上班</button>';
            break;

            case 'IN':
                $params['buttonGroup'] = 
                '<button type="submit" name="out_day" class="px-4 py-2 rounded-md bg-red-700 text-white">下班</button>
                <button type="submit" name="out_break_1" class="px-4 py-2 rounded-md bg-yellow-500 text-white">第一次休息</button>';
            break;

            case 'OUT_BREAK_1':
                $params['buttonGroup'] = 
                '<button type="submit" name="in_day" class="px-4 py-2 rounded-md bg-green-500 text-white">上班</button>';
            break;
            
            default:
                $params['buttonGroup'] = 
                    '<button type="submit" name="in_day" class="px-4 py-2 rounded-md bg-green-500 text-white">上班</button>';
            break;

        }

        echo $params['buttonGroup'];

    }

    // return $params;

}

function readOnlyTimesheet($params)
{

    $params = filterParams($params);
    $params['results'] = userPunchData($params);

    foreach ($params['results'] as $row)
    {

        $params['timesheetRow'] =
        '<tr>
            <td class="whitespace-nowrap px-4 py-2 font-medium text-gray-900 text-center">'.date('D m/d/Y', strtotime($row['punch_day'])).'</td>
            <td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['in_day'])).'</td>
            <td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A',

<details>
<summary>英文:</summary>

Thank you to hakre &amp; imvain2 for your comments on my question and giving some pointers. I was able to solve it by reading imvain2&#39;s answer.

What I did to fix the issue:

 - Since I have echo&#39;s in my functions(), I decided to test and comment out the echo&#39;s that I am running in my index.php

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-html --&gt;

    &lt;section class=&quot;px-4 py-6&quot;&gt;
      &lt;div class=&quot;container mx-auto space-y-4&quot;&gt;
        &lt;div class=&quot;flex flex-col items-center justify-center space-y-1&quot;&gt;
          &lt;div id=&quot;time&quot; class=&quot;font-semibold text-3xl&quot;&gt;&lt;/div&gt;
          &lt;div id=&quot;date&quot;&gt;&lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;flex items-center justify-center space-x-4&quot;&gt;
          &lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
            &lt;?php 
                        
                        $params = buttonGroup($params);

                        // echo $params[&#39;buttonGroup&#39;];
                        
                        ?&gt;
          &lt;/form&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/section&gt;
    &lt;section class=&quot;px-4 py-6&quot;&gt;
      &lt;div class=&quot;container mx-auto w-2/3 space-y-4&quot;&gt;
        &lt;form action=&quot;&quot; method=&quot;post&quot; class=&quot;w-1/3 flex flex-row items-end space-x-4&quot;&gt;
          &lt;div&gt;
            &lt;label for=&quot;HeadlineAct&quot; class=&quot;block text-sm font-medium text-gray-900&quot;&gt;
                            Select Pay-Period:
                        &lt;/label&gt;
            &lt;select name=&quot;pay_period&quot; id=&quot;HeadlineAct&quot; class=&quot;mt-1.5 p-2 w-full rounded-lg border-gray-300 text-gray-700 sm:text-sm&quot;&gt;
              &lt;?php

                            $params = viewPayPeriods($params);

                            // echo $params[&#39;payPeriodsList&#39;];

                            ?&gt;
            &lt;/select&gt;
          &lt;/div&gt;
          &lt;button type=&quot;submit&quot; name=&quot;pp_view&quot; class=&quot;rounded-lg bg-blue-600 px-5 py-2 font-medium text-white&quot;&gt;View&lt;/button&gt;
        &lt;/form&gt;
        &lt;div class=&quot;rounded-lg border border-gray-200 overflow-hidden&quot;&gt;
          &lt;table class=&quot;w-full divide-y-2 divide-gray-200 bg-white text-sm&quot;&gt;
            &lt;thead&gt;
              &lt;tr&gt;
                &lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
                  Date
                &lt;/th&gt;
                &lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
                  In Day
                &lt;/th&gt;
                &lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
                  Out Break
                &lt;/th&gt;
                &lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
                  In Break
                &lt;/th&gt;
                &lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
                  Out Lunch
                &lt;/th&gt;
                &lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
                  In Lunch
                &lt;/th&gt;
                &lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
                  Out Break
                &lt;/th&gt;
                &lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
                  In Break
                &lt;/th&gt;
                &lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
                  Out Day
                &lt;/th&gt;
                &lt;th class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900&quot;&gt;
                  Total Hours
                &lt;/th&gt;
              &lt;/tr&gt;
            &lt;/thead&gt;

            &lt;tbody class=&quot;divide-y divide-gray-200&quot;&gt;
              &lt;?php

                            $params = readOnlyTimesheet($params);

                            // echo $params[&#39;timesheetRow&#39;];

                            ?&gt;
            &lt;/tbody&gt;
          &lt;/table&gt;
        &lt;/div&gt;
        &lt;div class=&quot;flex flex-row items-center justify-end&quot;&gt;
          &lt;span class=&quot;font-medium&quot;&gt;Total Hours worked: &lt;?php $params = viewTotalHours($params); echo $params[&#39;totalHours&#39;]; ?&gt;&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/section&gt;

&lt;!-- end snippet --&gt;

And imvain2 mentions that while having echo&#39;s in my function (readOnlyTimesheet), I have `return $params`; and points out that I am not returning anything. I appreciate you for pointing that out. So after trying a couple things out, like commenting out my echo&#39;s in my `index.php` &amp; just utilizing what I have in my functions() and getting rid of my `return $params`, I got [this][1].

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-html --&gt;

    function buttonGroup($params)
    {

        $params = filterParams($params);
        $params = getEmpStatus($params);

        foreach ($params[&#39;results&#39;] as $row)
        {

            switch ($row[&#39;emp_status&#39;])
            {

                case &#39;OUT&#39;:
                    $params[&#39;buttonGroup&#39;] = 
                    &#39;&lt;button type=&quot;submit&quot; name=&quot;in_day&quot; class=&quot;px-4 py-2 rounded-md bg-green-500 text-white&quot;&gt;In Day&lt;/button&gt;&#39;;
                break;

                case &#39;IN&#39;:
                    $params[&#39;buttonGroup&#39;] = 
                    &#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
                    &lt;button type=&quot;submit&quot; name=&quot;out_break_1&quot; class=&quot;px-4 py-2 rounded-md bg-yellow-500 text-white&quot;&gt;Out Break&lt;/button&gt;&#39;;
                break;

                case &#39;OUT_BREAK_1&#39;:
                    $params[&#39;buttonGroup&#39;] = 
                    &#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
                    &lt;button type=&quot;submit&quot; name=&quot;in_break_1&quot; class=&quot;px-4 py-2 rounded-md bg-green-500 text-white&quot;&gt;In Break&lt;/button&gt;&#39;;
                break;
                
                case &#39;IN_BREAK_1&#39;:
                    $params[&#39;buttonGroup&#39;] = 
                    &#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
                    &lt;button type=&quot;submit&quot; name=&quot;out_lunch&quot; class=&quot;px-4 py-2 rounded-md bg-blue-700 text-white&quot;&gt;Out Lunch&lt;/button&gt;&#39;;
                break;

                case &#39;OUT_LUNCH&#39;:
                    $params[&#39;buttonGroup&#39;] = 
                    &#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
                    &lt;button type=&quot;submit&quot; name=&quot;in_lunch&quot; class=&quot;px-4 py-2 rounded-md bg-green-500 text-white&quot;&gt;In Lunch&lt;/button&gt;&#39;;
                break;

                case &#39;IN_LUNCH&#39;:
                    $params[&#39;buttonGroup&#39;] = 
                    &#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
                    &lt;button type=&quot;submit&quot; name=&quot;out_break_2&quot; class=&quot;px-4 py-2 rounded-md bg-yellow-500 text-white&quot;&gt;Out Break&lt;/button&gt;&#39;;
                break;

                case &#39;OUT_BREAK_2&#39;:
                    $params[&#39;buttonGroup&#39;] = 
                    &#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;
                    &lt;button type=&quot;submit&quot; name=&quot;in_break_2&quot; class=&quot;px-4 py-2 rounded-md bg-green-500 text-white&quot;&gt;In Break&lt;/button&gt;&#39;;
                break;

                case &#39;IN_BREAK_2&#39;:
                    $params[&#39;buttonGroup&#39;] = 
                    &#39;&lt;button type=&quot;submit&quot; name=&quot;out_day&quot; class=&quot;px-4 py-2 rounded-md bg-red-700 text-white&quot;&gt;Out Day&lt;/button&gt;&#39;;
                break;

                default:
                    $params[&#39;buttonGroup&#39;] = 
                        &#39;&lt;button type=&quot;submit&quot; name=&quot;in_day&quot; class=&quot;px-4 py-2 rounded-md bg-green-500 text-white&quot;&gt;In Day&lt;/button&gt;&#39;;
                break;

            }

            echo $params[&#39;buttonGroup&#39;];

        }

        // return $params;

    }

    function readOnlyTimesheet($params)
    {

        $params = filterParams($params);
        $params[&#39;results&#39;] = userPunchData($params);

        foreach ($params[&#39;results&#39;] as $row)
        {

            $params[&#39;timesheetRow&#39;] =
            &#39;&lt;tr&gt;
                &lt;td class=&quot;whitespace-nowrap px-4 py-2 font-medium text-gray-900 text-center&quot;&gt;&#39;.date(&#39;D m/d/Y&#39;, strtotime($row[&#39;punch_day&#39;])).&#39;&lt;/td&gt;
                &lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;in_day&#39;])).&#39;&lt;/td&gt;
                &lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;out_break_1&#39;])).&#39;&lt;/td&gt;
                &lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;in_break_1&#39;])).&#39;&lt;/td&gt;
                &lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;out_lunch&#39;])).&#39;&lt;/td&gt;
                &lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;in_lunch&#39;])).&#39;&lt;/td&gt;
                &lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;out_break_2&#39;])).&#39;&lt;/td&gt;
                &lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;in_break_2&#39;])).&#39;&lt;/td&gt;
                &lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.date(&#39;H:i:s A&#39;, strtotime($row[&#39;out_day&#39;])).&#39;&lt;/td&gt;
                &lt;td class=&quot;whitespace-nowrap px-4 py-2 text-gray-700 text-center&quot;&gt;&#39;.$row[&#39;total_hours&#39;].&#39;&lt;/td&gt;
            &lt;/tr&gt;&#39;;

            echo $params[&#39;timesheetRow&#39;];

        }

        // return $params;

    }

    function viewPayPeriods($params)
    {

        $params = filterParams($params);
        $params[&#39;results&#39;] = getPayPeriods($params);

        foreach ($params[&#39;results&#39;] as $row)
        {

            $params[&#39;payPeriodsList&#39;] =
            &#39;&lt;option value=&quot;&#39;.$row[&#39;id&#39;].&#39;&quot;&gt;&#39;.date(&#39;m/d/Y&#39;, strtotime($row[&#39;pp_start&#39;])).&#39;&#39;.&#39; - &#39;.&#39;&#39;.date(&#39;m/d/Y&#39;, strtotime($row[&#39;pp_end&#39;])).&#39;&lt;/option&gt;&#39;;

            echo $params[&#39;payPeriodsList&#39;];

        }

        // return $params;

    }

&lt;!-- end snippet --&gt;

Thank you once again hakre &amp; imvain2! I appreciate you taking the time reading my question and pointing out some errors within my code. It&#39;s always refreshing seeing people in the community help each other out.

  [1]: https://i.stack.imgur.com/nxPtq.png

</details>



# 答案2
**得分**: -2

根据提供的代码,似乎问题出在您使用`foreach`循环的方式上。每个`foreach`循环都在覆盖`$params`数组的值,这导致在同一页上使用多个循环时出现冲突。

为了解决这个问题,您可以修改代码,将每个循环的结果存储在`$params`数组内的单独数组中。以下是如何修改您的代码的示例:

```php
function buttonGroup($params)
{
    $params = filterParams($params);
    $params = getEmpStatus($params);

    $buttonGroup = array(); // 创建一个新数组来存储按钮组

    foreach ($params['results'] as $row) {
        switch ($row['emp_status']) {
            case 'OUT':
                $buttonGroup[] = 'In Day';
                break;
            case 'IN':
                $buttonGroup[] = 'Out Day Out Break';
                break;
            // ... 其他情况 ...
        }
    }

    $params['buttonGroup'] = $buttonGroup; // 将按钮组存储在$params数组中

    return $params;
}

function viewPayPeriods($params)
{
    $params = filterParams($params);
    $params['results'] = getPayPeriods($params);

    $payPeriodsList = array(); // 创建一个新数组来存储薪资期间

    foreach ($params['results'] as $row) {
        $payPeriodsList[] = date('m/d/Y', strtotime($row['pp_start'])) . ' - ' . date('m/d/Y', strtotime($row['pp_end']));
    }

    $params['payPeriodsList'] = $payPeriodsList; // 将薪资期间存储在$params数组中

    return $params;
}

function readOnlyTimesheet($params)
{
    $params = filterParams($params);
    $params['results'] = userPunchData($params);

    $timesheetRows = array(); // 创建一个新数组来存储工作时间表的行

    foreach ($params['results'] as $row) {
        $timesheetRows[] = array(
            'date' => date('D m/d/Y', strtotime($row['punch_day'])),
            'in_day' => date('H:i:s A', strtotime($row['in_day'])),
            // ... 其他字段 ...
        );
    }

    $params['timesheetRows'] = $timesheetRows; // 将工作时间表的行存储在$params数组中

    return $params;
}

然后,在您的HTML代码中,您可以使用修改后的键访问存储的值:

<?php
$params = buttonGroup($params);
foreach ($params['buttonGroup'] as $button) {
    echo $button;
}
?>

<!-- ... -->

<?php
$params = viewPayPeriods($params);
foreach ($params['payPeriodsList'] as $payPeriod) {
    echo $payPeriod;
}
?>

<!-- ... -->

<?php
$params = readOnlyTimesheet($params);
foreach ($params['timesheetRows'] as $row) {
    echo $row['date'];
    echo $row['in_day'];
    // ... 其他字段 ...
}
?>

通过将每个循环的结果存储在$params数组内的单独数组中,您可以避免冲突,并在页面上正确显示数据。

英文:

Based on the code provided, it seems like the issue is with the way you are using the foreach loops. Each foreach loop is overwriting the values of the $params array, which is causing conflicts when you try to use multiple loops on the same page.

To fix this issue, you can modify your code to store the results of each loop in separate arrays within the $params array. Here's an example of how you can modify your code:

function buttonGroup($params)
{
$params = filterParams($params);
$params = getEmpStatus($params);
$buttonGroup = array(); // Create a new array to store the button groups
foreach ($params[&#39;results&#39;] as $row) {
switch ($row[&#39;emp_status&#39;]) {
case &#39;OUT&#39;:
$buttonGroup[] = &#39;In Day&#39;;
break;
case &#39;IN&#39;:
$buttonGroup[] = &#39;Out Day Out Break&#39;;
break;
// ... other cases ...
}
}
$params[&#39;buttonGroup&#39;] = $buttonGroup; // Store the button groups in the $params array
return $params;
}
function viewPayPeriods($params)
{
$params = filterParams($params);
$params[&#39;results&#39;] = getPayPeriods($params);
$payPeriodsList = array(); // Create a new array to store the pay periods
foreach ($params[&#39;results&#39;] as $row) {
$payPeriodsList[] = date(&#39;m/d/Y&#39;, strtotime($row[&#39;pp_start&#39;])) . &#39; - &#39; . date(&#39;m/d/Y&#39;, strtotime($row[&#39;pp_end&#39;]));
}
$params[&#39;payPeriodsList&#39;] = $payPeriodsList; // Store the pay periods in the $params array
return $params;
}
function readOnlyTimesheet($params)
{
$params = filterParams($params);
$params[&#39;results&#39;] = userPunchData($params);
$timesheetRows = array(); // Create a new array to store the timesheet rows
foreach ($params[&#39;results&#39;] as $row) {
$timesheetRows[] = array(
&#39;date&#39; =&gt; date(&#39;D m/d/Y&#39;, strtotime($row[&#39;punch_day&#39;])),
&#39;in_day&#39; =&gt; date(&#39;H:i:s A&#39;, strtotime($row[&#39;in_day&#39;])),
// ... other fields ...
);
}
$params[&#39;timesheetRows&#39;] = $timesheetRows; // Store the timesheet rows in the $params array
return $params;
}

Then, in your HTML code, you can access the stored values using the modified keys:

&lt;?php
$params = buttonGroup($params);
foreach ($params[&#39;buttonGroup&#39;] as $button) {
echo $button;
}
?&gt;
&lt;!-- ... --&gt;
&lt;?php
$params = viewPayPeriods($params);
foreach ($params[&#39;payPeriodsList&#39;] as $payPeriod) {
echo $payPeriod;
}
?&gt;
&lt;!-- ... --&gt;
&lt;?php
$params = readOnlyTimesheet($params);
foreach ($params[&#39;timesheetRows&#39;] as $row) {
echo $row[&#39;date&#39;];
echo $row[&#39;in_day&#39;];
// ... other fields ...
}
?&gt;

By storing the results of each loop in separate arrays within the $params array, you can avoid conflicts and display the data correctly on the page.

huangapple
  • 本文由 发表于 2023年7月18日 02:59:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定