PHP List Plugin - Apply code only to the first selected

tiagovareta

Member
Hi,

I have a list button (php list plugin), to synchronize the list records, in the MailChimp list.
It is working, but only synchronizes the 1st selected record.
I have the following code:

$app = JFactory::getApplication();
$ids = $app->input->get('ids', array(), 'array');

foreach ($ids AS $myid)
{
$row = $model->getRow($myid);
$email = $row->tb_contactos_geral___email_login_ctg;
$nome = $row->tb_contactos_geral___nome_ctg;
$apelido = $row->tb_contactos_geral___apelido_ctg;
$status_ori = $row->tb_contactos_geral___status_ctg;
$status = "subscribed";

$apiKey = 'myapikey';
$listId = 'mylistid';

$memberId = md5(strtolower($email));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
$json = json_encode([
'email_address' => $email,
'status' => $status, // "subscribed","unsubscribed","cleaned","pending"
'merge_fields' => [
'FNAME' => $nome,
'LNAME' => $apelido,
'STATUSORI' => $status_ori,
]
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpCode;
}

What could I be doing wrong?
 
You've got a 'return' as the last line inside your foreach() loop, so when it gets to that line on the first time trhough the loop, it returns.

Rather than returning, you should probably check the response, see if it returned an error, optionally give some user feedback (or log the error) and carry on.

-- hugh
 
You've got a 'return' as the last line inside your foreach() loop, so when it gets to that line on the first time trhough the loop, it returns.

Rather than returning, you should probably check the response, see if it returned an error, optionally give some user feedback (or log the error) and carry on.

-- hugh

Hi Cheesegrits!

Thank you for the tip!!! I removed the line: return $httpCode;
And it seems to be working fine!

Thank you so much!
 
I'm testing the indicated code, but nothing happens. I placed the code in the OnShowinList event.
Any suggestions?
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top