0

So, I'm trying to iterate through a foreach loop of all the entries to a gravity forms form. I have it sort of working in a template, but I need to put it in a class (creating a calendar and need to pull in entry information) and now it is just iterating over the last entry a bunch of times. Not sure where I went wrong:

    $form_id = 9; //Enter the form ID here. The PTO form ID is 9;
    $search_criteria = array();
    $sorting         = array();
    $paging          = array( 'offset' => 0, 'page_size' => 25 );
    $total_count     = 0;
    $entry           = GFAPI::get_entries( $form_id, $search_criteria, $sorting, $paging, $total_count );   

    foreach ( $entry as $entries ) {

        $entry_id     = rgar( $entries, 'id' );
        $fname        = rgar( $entries, '13.3' );
        $lname        = rgar( $entries, '13.6' );
        $pto_start    = rgar( $entries, '1' );
        $pto_end      = rgar( $entries, '3' );
        $e_location   = rgar( $entries, '16' );
        $pto_approval = rgar( $entries, '15' );


        $supervisor_location = um_user('employee_location');


        if ( ($supervisor_location === 'Elk City, OK') && ($e_location === 'Elk City, OK' ) ) { 
            if ( $pto_start != $this->currentDate ) {
                return '<div id="'.$this->currentDate.'" class="day '.($cellNumber%7==1?' start ':($cellNumber%7==0?' end ':' ')).
                    ($cellContent==null?'mask':'').'">'.$cellContent.'</div>';
            }
            elseif ( $pto_start == $this->currentDate ) {
                return '<div id="'.$this->currentDate.'" class="day has-pto '.($cellNumber%7==1?' start ':($cellNumber%7==0?' end ':' ')).
                    ($cellContent==null?'mask':'').'">'.$cellContent.'<div class="inner-pto-cal '.lcfirst($pto_approval).'">'.$fname. ' '.$lname.'</div></div>';
            }
        }
    }

1 Answer 1

1

Not sure how relevant the answer is now, but it might help someone in future.

  1. It would be advisable to switch $entries and $entry. It's quite confusing that you use $entries for singular entry and $entry for an array of entries.

  2. Define what output you are looking for. You are looking to get an array, but you're rewriting your values with each loop - that's why the last one stands- like you're repainting a house and wondering why only the top color can be seen...

  3. I'd say you want to do this to the $calendar_data array and an $outputx output:

    $calendar_data = array();
    
    $outputx = "";
    
    $entries = GFAPI::get_entries($form_id, $search_criteria, $sorting, $paging, $total_count);
    
    foreach ($entries as $entry) {
        $idx = rgar($entry, 'id');
        $calendar_data[$idx]["fname"] = rgar($entry, '13.3');
        $calendar_data[$idx]["lname"] = rgar($entry, '13.6');
        $calendar_data[$idx]["pto_start"] = rgar($entry, '1');
        $calendar_data[$idx]["pto_end"] = rgar($entry, '3');
        $calendar_data[$idx]["e_location"] = rgar($entry, '16');
        $calendar_data[$idx]["pto_approval"] = rgar($entry, '15');
    
        $supervisor_location = um_user('employee_location');
    
        if (($supervisor_location === 'Elk City, OK') && ($e_location === 'Elk City, OK' )) {
            if ($pto_start != $this->currentDate) {
                $outputx .= '<div id="' . $this->currentDate . '" class="day ' . ($cellNumber % 7 == 1 ? ' start ' : ($cellNumber % 7 == 0 ? ' end ' : ' ')) .
                ($cellContent == null ? 'mask' : '') . '">' . $cellContent . '</div>';
            } elseif ($pto_start == $this->currentDate) {
                $outputx .= '<div id="' . $this->currentDate . '" class="day has-pto ' . ($cellNumber % 7 == 1 ? ' start ' : ($cellNumber % 7 == 0 ? ' end ' : ' ')) .
                ($cellContent == null ? 'mask' : '') . '">' . $cellContent . '<div class="inner-pto-cal ' . lcfirst($pto_approval) . '">' . $fname . ' ' . $lname . '</div></div>';
            }
        }
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.