Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listSubscribe in Groups Mailchimp API 1.3

Tags:

php

mailchimp

As the example below shows how to call on the fields, my question is how to call a multiple checked checkbox. please give me an example

   $merge_vars = array('FNAME'=>'Test', 'LNAME'=>'Account', 
              'GROUPINGS'=>array(
                    array('name'=>'Your Interests:', 'groups'=>'Bananas,Apples'),
                    array('id'=>22, 'groups'=>'Trains'),
                    )
                );

I get a solution for this.

To get the multiple checked checkbox you need to do a looping and set it in array then change the array to a string.

 if(!empty($_POST['listbox']))
    {
        foreach($_POST['listbox'] as $value => $val)
        {

            $values[] = $val;

        }
         $groups = implode(",", $values);
    } 

then set it in the merge_vars

 $merge_vars = array('FNAME'=>'Test', 'LNAME'=>'Account', 
          'GROUPINGS'=>array(
                array('name'=>'Your Interests:', 'groups'=> $groups)
                )
            );

Hope it helps :)

like image 699
GianFS Avatar asked Oct 11 '11 12:10

GianFS


1 Answers

You must put the separated by commas but you must ensure that they have commas escaped:

$groups = array();
if(!empty($_POST['listbox'])) {
    $interests = array();
    foreach($_POST['listbox'] as $interest)
    {
        $interests[] = str_replace(',', '\,', $interest);
    }

    $groups = implode(",", $interests);
}

$merge_vars = array(
    'FNAME'=>'Test', 
    'LNAME'=>'Account', 
    'GROUPINGS'=> array(
        array(
            'name'=>'Your Interests:', 
            'groups'=> $groups
        ),
        array(
            'id'=>22, 
            'groups'=>'Trains'
        )
    )
);

If you are sure that the interest string do not have commas you can just do this:

$groups = implode(',', $_POST['listbox']);
like image 180
artberri Avatar answered Oct 20 '22 00:10

artberri