Foreach循环遍历重力表单条目

时间:2019-04-23 作者:Ben Calhoun

所以,我尝试遍历一个foreach循环,该循环包含重力表单的所有条目。我有一个模板,但我需要把它放在一个类中(创建日历并需要拉入条目信息),现在它只是在上一个条目上迭代了很多次。不知道我哪里出错了:

    $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 个回复
SO网友:Mo Pristas

现在还不确定这个答案有多重要,但它可能会在将来对某人有所帮助。

建议切换$entries$entry. 你用的东西很让人困惑$entries 对于单数输入和$entry 用于条目数组。

定义您想要的输出。你想得到一个数组,但你正在用每个循环重写你的值-这就是为什么最后一个循环仍然存在-就像你在重新粉刷房子,想知道为什么只能看到顶部的颜色。。。

我想说你想对$calendar_data 数组和an$outputx 输出:

$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>\';
        }
    }
}

相关推荐