0

I am populating a wordpress gravity form using one of there pre render filters.

I follow their documentation normally and I dynamically populate $items[] using a foreach loop which works every time.

But this time I am not dynamically making a php foreach loop for the $items[] array, I am manually inputting each value into an array.

Please see below...

add_filter("gform_pre_render", "populate_dropdown_bike_make");
add_filter("gform_admin_pre_render", "populate_dropdown_bike_make");
function populate_dropdown_bike_make($form){
    if($form["id"] != 4)
        return $form;

    $items = array();
    $items[] = array(
        "text" => "", "value" => null,
        "text" => "YAMASAKI", "value" => "YAMASAKI",
        "text" => "YAMOTO", "value" => "YAMOTO",
        "text" => "YEZDI", "value" => "YEZDI",
        "text" => "YIBEN", "value" => "YIBEN",
        "text" => "YIYING", "value" => "YIYING",
        "text" => "YONGKANG", "value" => "YONGKANG",
        "text" => "YONGWANG", "value" => "YONGWANG",
        "text" => "ZEBRETTA", "value" => "ZEBRETTA",
        "text" => "ZENNCO", "value" => "ZENNCO",
        "text" => "ZEPII", "value" => "ZEPII",
        "text" => "ZERO-MOTORCYCLES", "value" => "ZERO-MOTORCYCLES",
        "text" => "ZEV", "value" => "ZEV",
        "text" => "ZHEJIANG", "value" => "ZHEJIANG",
        "text" => "ZHENHUA", "value" => "ZHENHUA",
        "text" => "ZHIXI", "value" => "ZHIXI",
        "text" => "ZHONGYU", "value" => "ZHONGYU",
        "text" => "ZING", "value" => "ZING",
        "text" => "ZIPPI", "value" => "ZIPPI",
        "text" => "ZIPSTAR", "value" => "ZIPSTAR",
        "text" => "ZONGSHEN", "value" => "ZONGSHEN",
        "text" => "ZONTES", "value" => "ZONTES",
        "text" => "ZUNDAPP", "value" => "ZUNDAPP"   
    );

    foreach($form["fields"] as &$field)
        if($field["id"] == 45){           
            $field["choices"] = $items;
        }

    return $form;

}


Now the problem with this is it is only returning the very last option ZUNDAPP in the dropdown.

Can anyone see why this is happening?

Thanks

1
  • 1
    Your array has only two entries, one with the key of text, and one with the key of value. You over-write the value on every line, so at the end, ZUNDAPP is the only value in there. Commented Sep 25, 2013 at 15:59

4 Answers 4

4

You're redefining the same keys over and over.

$items[] = array(
    "text" => "", "value" => null,
    "text" => "YAMASAKI", "value" => "YAMASAKI"
)

Gives:

array(array(
    "text" => "YAMASAKI",
    "value" => "YAMASAKI"
));

In think you intend:

//    v-- no [] here
$items = array(
    array("text" => "", "value" => null),
    array("text" => "YAMASAKI", "value" => "YAMASAKI"),
    // etc.
Sign up to request clarification or add additional context in comments.

Comments

2

I think the problem relies in your built array structure.

You only add one element (which is an array). In your array itself, you overwrite all previous keys by redefining them.

Use this syntax:

$items[] = array("text" => "", "value" => null);
$items[] = array("text" => "YAMASAKI", "value" => "YAMASAKI");

You can also directly assign the array:

$items = array(
    array("text" => "", "value" => null),
    array("text" => "YAMASAKI", "value" => "YAMASAKI"),
    // ...  
);

3 Comments

OK I guess that make sense, I was not sure if thier was a shorter way of encapsulating it
I was just about to say did you need array on each line like fritz wrote. Thanks
@Joshc Yes, you need that (my now edited comment was wrong, sorry). Otherwise, you won't insert an array of arrays of data values.
0
$items[] = array(
      ^^

There's your problem. This is causing your array to turn into a multidimensional array. Your code assumes it's still an one-dimensional array and that's the reason why it outputs only the last element.

Change it to:

$items = array(

For example:

$arr = array();
$arr[] = array('foo' => 'bar');
print_r($arr);

According to your logic, the output should be:

Array
(
    [foo] => bar
)

But it's actually:

Array
(
    [0] => Array
        (
            [foo] => bar
        )

)

Comments

0

Your are overwriting text and value everytime you insert something into $items.

Your would likely need to insert values such as

$items = array( 'key' => 'value', .... )

Or wrap the individual entries in an array, per the documention:

$items = array (
            array ('value' => value1, 'key' => key1 ),
            array ('value' => value2, 'key' => key2 ),
            ....
         );

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.