英文:
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 & 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 "In Day" 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 `<table>` as well. However, after reloading the page, the live clock disappears and the employee punch data doesn't load, along with the list of pay-periods under the `<select>` 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 & *voila*, the employee punch data for the timesheet *displays*. But now, that leaves the list of pay periods under the `<select>` 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'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['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 Loop that pulls in list of pay periods**
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 Loop that pulls in employee punch data**
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', strtotime($row['out_break_1'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['in_break_1'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['out_lunch'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['in_lunch'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['out_break_2'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['in_break_2'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['out_day'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.$row['total_hours'].'</td>
</tr>';
echo $params['timesheetRow'];
}
}
**Home Page that loads in data as described above**
<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">
Select Pay-Period:
</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">View</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">
Date
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
In Day
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
Out Break
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
In Break
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
Out Lunch
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
In Lunch
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
Out Break
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
In Break
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
Out Day
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
Total Hours
</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">Total Hours worked: <?php $params = viewTotalHours($params); echo $params['totalHours']; ?></span>
</div>
</div>
</section>
</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 & imvain2 for your comments on my question and giving some pointers. I was able to solve it by reading imvain2's answer.
What I did to fix the issue:
- Since I have echo's in my functions(), I decided to test and comment out the echo's that I am running in my index.php
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-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">
Select Pay-Period:
</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">View</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">
Date
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
In Day
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
Out Break
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
In Break
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
Out Lunch
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
In Lunch
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
Out Break
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
In Break
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
Out Day
</th>
<th class="whitespace-nowrap px-4 py-2 font-medium text-gray-900">
Total Hours
</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">Total Hours worked: <?php $params = viewTotalHours($params); echo $params['totalHours']; ?></span>
</div>
</div>
</section>
<!-- end snippet -->
And imvain2 mentions that while having echo'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's in my `index.php` & just utilizing what I have in my functions() and getting rid of my `return $params`, I got [this][1].
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
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;
default:
$params['buttonGroup'] =
'<button type="submit" name="in_day" class="px-4 py-2 rounded-md bg-green-500 text-white">In Day</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', strtotime($row['out_break_1'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['in_break_1'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['out_lunch'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['in_lunch'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['out_break_2'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['in_break_2'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.date('H:i:s A', strtotime($row['out_day'])).'</td>
<td class="whitespace-nowrap px-4 py-2 text-gray-700 text-center">'.$row['total_hours'].'</td>
</tr>';
echo $params['timesheetRow'];
}
// return $params;
}
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'];
}
// return $params;
}
<!-- end snippet -->
Thank you once again hakre & imvain2! I appreciate you taking the time reading my question and pointing out some errors within my code. It'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['results'] as $row) {
switch ($row['emp_status']) {
case 'OUT':
$buttonGroup[] = 'In Day';
break;
case 'IN':
$buttonGroup[] = 'Out Day Out Break';
break;
// ... other cases ...
}
}
$params['buttonGroup'] = $buttonGroup; // Store the button groups in the $params array
return $params;
}
function viewPayPeriods($params)
{
$params = filterParams($params);
$params['results'] = getPayPeriods($params);
$payPeriodsList = array(); // Create a new array to store the pay periods
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; // Store the pay periods in the $params array
return $params;
}
function readOnlyTimesheet($params)
{
$params = filterParams($params);
$params['results'] = userPunchData($params);
$timesheetRows = array(); // Create a new array to store the timesheet rows
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'])),
// ... other fields ...
);
}
$params['timesheetRows'] = $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:
<?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'];
// ... other fields ...
}
?>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论