检查Gravity Forms

时间:2015-08-12 19:12:37

标签: php mysql wordpress gravity-forms-plugin

我正在使用扩展Gravity Forms的WordPress插件。我需要添加一些功能来检查重复的条目。我使用GFAPI::get_entries( $form_id, $search_criteria );查找并列出了符合特定搜索条件的与Gravity表单相关联的所有条目。这是代码的片段:

// Fetch field_ids, establish search criteria
    $first_id = $last_id = $address_id = 0;
    foreach ( $form['fields'] as $field ) {
        if ( $field->get_input_type() == 'name' ) {
            $first_id = $field->id . '.3';
            $last_id  = $field->id . '.6';
            break;
        }
    }
    foreach ( $form['fields'] as $field ) {
        if ( $field->get_input_type() == 'address' ) {
            $address_id = $field->id . '.1';
            break;
        }
    }
    $search_criteria['field_filters'] = array(
        array( 'key' => $first_id, 'value' => $data['voterdata_FirstName'] ),
        array( 'key' => $last_id, 'value' => $data['voterdata_LastName'] ),
        array( 'key' => $address_id, 'value' => $data['voterdata_VoterAddress'] )
    );
    $search_criteria['status'] = 'active';
    $entries = GFAPI::get_entries( $form_id, $search_criteria );

var_dump(); $entries会返回符合搜索条件的表单的所有条目。如果存在多个包含相同名字,姓氏和地址组合的条目,我想将我的用户重定向到另一个页面,以便他们可以确认哪个条目属于他们。检查$entries重复项的最佳方法是什么?

提前致谢,

2 个答案:

答案 0 :(得分:1)

您可以使用in_array

示例:

<?php
$results = [
    ['firstName' => 'Bob', 'lastName' => 'Dole', 'address' => '123 Test'],
    ['firstName' => 'Jon', 'lastName' => 'Doe', 'address' => '444 Foo'],
    ['firstName' => 'Bob', 'lastName' => 'Dole', 'address' => '333 Test']
];

$test = ['firstName' => 'Bob', 'lastName' => 'Dole', 'address' => '123 Test'];

$test2 = ['firstName' => 'Jon', 'lastName' => 'Dole', 'address' => '999 Test'];

$test3 = ['lastName' => 'Jon', 'firstName' => 'Dole', 'address' => '999 Test'];

var_dump(in_array($test, $results));   // This should match
var_dump(in_array($test2, $results));  // This should not match
var_dump(in_array($test3, $results));  // This should not match

输出:

bool(true)
bool(false)
bool(false)

修改

刚刚意识到您已经在搜索...在这种情况下,如果wp_redirect()不为空,您可以使用$entries ...我假设您已经在插入之前检查数据,对吗?如果没有,你应该。

答案 1 :(得分:0)

我发现了一个解决方案:

if ( count( array_unique( $entries ) ) < count( $entries ) ) {
        # do something ...
    }

当然,这不是唯一的方法。但希望它可以帮助别人!