skip to Main Content

I am creating a personal project which is a time punch application. On this page (refer to screenshot), 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>

2

Answers


  1. Chosen as BEST ANSWER

    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

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

    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.

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

    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.


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search