PHP ERROR Message: Invalid argument supplied for foreach() WHEN i implementasion date filter

PHP ERROR Message: Invalid argument supplied for foreach() WHEN i implementasion date filter

please help me, here I want to use filter by date using datepicker
but the error occurred "Message: Invalid argument supplied for foreach ()"

this is my code:

model:

    public function get_allDprtDetail($DepartmentID)
    {
        if(isset($_GET['tanggal'])){
            $tgl = $_GET['tanggal'];

        $datas = "
        SELECT 
....
    IF
        ( emp0003.NIK = v_dvc0004_test.NIK, 'Hadir', 'Tidak Hadir' ) AS Attendance 
    FROM
        emp0001
        INNER JOIN emp0003 ON emp0003.DepartmentID = emp0001.DepartmentID
        
LEFT JOIN v_dvc0004_test ON emp0003.NIK = v_dvc0004_test.NIK *AND DATE(v_dvc0004_test.Enroll) = '$tgl'
        JOIN emp0002 ON emp0002.PositionID = emp0003.PositionID
        LEFT JOIN shift ON emp0003.Shift_ID = shift.Shift_ID 
    WHERE
        emp0001.DepartmentID = '$DepartmentID' 
        AND emp0003.IsActive = 'T' 
    ORDER BY
        v_dvc0004_test.Enroll ASC
    
    ";
        $query = $this->db->query($datas);
        return $query->result();}
    }

view:


     </div>
    
                                        <form method="get">
                <label>PILIH TANGGAL</label>
                <input type="date" name="tanggal">
                <input type="submit" value="FILTER">
            </form>  
                                ....
    
                            <tbody>
                                <?php foreach ($getDepartDetail as $us) { ?>
                                    <tr>
                                        <td><?php echo $us->Name ?></td>
                                        <td><?php echo $us->NIK ?></td>
                                        <td><?php echo $us->gender ?></td>
                                        <td><?php echo $us->PositionDesc ?></td>
                                        <td><?php echo $us->Shift ?></td>
                                        <td><?php echo $us->tgl ?></td>
                                        <td><?php echo $us->Attendance ?></td>
                                        <td><a class="btn btn-info" href="<?php echo site_url('data_detail/detail_datas/vieworc/' . $us->NIK); ?>">Detail</a></td>
                                    </tr>
                                <?php } ?>
                            </tbody>
                        ....

5 thoughts on “PHP ERROR Message: Invalid argument supplied for foreach() WHEN i implementasion date filter”

  1. Function get_allDprtDetail can return null if $_GET['tanggal'] doesn’t set set. You may try to return empty array, like that

     public function get_allDprtDetail($DepartmentID)
    {
        if(isset($_GET['tanggal'])){
           some code
        }
        return [];
    }
    
    Reply

Leave a Comment