Displaying the total of all values for a custom field (in a list)

Hi, hopefully someone can help me...

I have a hidden field in my subscribe form that collects a variable number (e.g. 200). This number is added as a custom field for each subscriber. I named this custom field "offset".

The same way that I would use <? echo $result->response->TotalActiveSubscribers; ?> to display the total number of subscribers on a website... What I want to do is display the combined total of all numbers (in my "offset" custom field) for every subscriber in the list.

So for example, if my list had 3 subscribers and their "offset" custom field numbers were 10, 8 and 8, I would want to display the total of those numbers (e.g. 10 + 8 + 8 = 26). Echoing "26" on the website!

Hopefully that makes sense :) Is this possible??

Thanks,
Andrew

roshodgekiss roshodgekiss, 2 years ago

Hi there Andrew, it's a little tricky describing a code solution, but bear with me - here's what I think you need to do to get the sum of numeric values within your subscribers' custom fields.

First of all, you'll need to use our active subscribers method, to get the values within your subscribers' custom fields. Once you add these values to an array, you will be able to get the sum of array values and display them on your site.

How you implement this really depends on the language you're comfortable using. You may want to check out our samples and wrappers, as these will likely come in useful for this task. Let us know how you go :)


Get in touch with us on Twitter: http://twitter.com/campaignmonitor
We're also on Facebook: http://facebook.com/campaignmonitor
roomfive, 2 years ago

Thanks Rosanne,

PHP is defo the language I'm using, although I'm a front-end dev, so it's hard to get my head around this :/ ...So what I'm really looking for is a line(s) of PHP code I can use to display this... Like in my example above to get all the subscribers: <? echo $result->response->TotalActiveSubscribers; ?>

Can anyone help?

Ken Ken, 2 years ago

Hi Andrew,

As Ros said, you will need to call the active subscribers method to grab your active subscribers, to loop through their data and sum up their values. There is an sample on how to call that method in the PHP wrapper.

Once you've retrieved your page of active subscribers, you can use the following code to loop through the subscribers, summing up the values:

$result = $wrap->get_active_subscribers('', 1, 50, 'email', 'asc');

if($result->was_successful()) {
    $sum = 0.0;
    foreach ($result->response->Results as $sub) {
        foreach ($sub->CustomFields as $cf) {
            if ($cf->Key == "[NumberField]") { // ENTER YOUR CUSTOM FIELD NAME HERE
                $sum += floatval($cf->Value);
            }
        }
    }
    var_dump($sum);
}

I would change the first line so that you only grab pages of 1,000 subscribers at a time, so that you don't grab too much data and possibly suffer a time out. I hope that makes sense.

Feel free to get back to us if you have any other problems or questions.


http://www.campaignmonitor.com
roomfive, 2 years ago

Hi Ken,

Thanks so much for your help :)

This looks to be what I want to achieve, but all it's returning on the front-end is float(0)

Maybe I have something else in my code conflicting...

I took your code and added my custom field name (Offset), do I need to change anything else? Below is stripped back version of my code to show only the CM API elements, maybe you can see where I'm going wrong... I've disguised the list IDs — I thought that best to do, but I can email you them if they help...

<?
require_once 'csrest_lists.php';
$wrap = new CS_REST_Lists('[hidden]', '[hidden]');
$result = $wrap->get_stats();
$first_number = 365;
$second_number = 0.365;
$now = time();
$your_date = strtotime("2015-10-01");
$datediff = $your_date-$now;
?>

<html>
<body>

<?
$total = $wrap->get_active_subscribers('', 1, 50, 'email', 'asc');

if($total->was_successful()) {
    $sum = 0.0;
    foreach ($total->response->Results as $sub) {
        foreach ($sub->CustomFields as $cf) {
            if ($cf->Key == "Offset") { // ENTER YOUR CUSTOM FIELD NAME HERE
                $sum += floatval($cf->Value);
            }
        }
    }
    var_dump($sum);
}
?>

</body>
</html>

Thanks,
Andrew

Ken Ken, 2 years ago

Hi Andrew,

The key for the custom fields is the not the field name, but the personalisation tag. So you'll need to use "[Offset]", not "Offset".

Give that a go, and let me know if it still doesn't work.


http://www.campaignmonitor.com
roomfive, 2 years ago

Thanks Ken :)

So close!!! It now returns the number, but is still says float(408) — how do I remover the "float" and brackets around the number?

roomfive, 2 years ago

Hold on, cracked it! I love you Ken ;)

I replaced the var_dump($sum); at the end with echo $sum;

Thanks so much for your time (and patience)

Ken Ken, 2 years ago

Awesome Andrew! I'm glad you got it working!

Let me know if there is anything else I can do for you.


http://www.campaignmonitor.com
roomfive, 2 years ago

Hey again Ken, or any developer, free...

I've discovered that you can only set the number of subscribers it counts to a limit of 1,000. The original code Ken posted above was set to 50, so I upped it to 1,000... It won't allow me to set it any higher than 1,000 or it breaks :(

...BUT there will certainly be significantly more than 1,000 subscribers... Is it possible to achieve this, so it counts ALL the subscribers in my list, regardless of it's size?

My code:

<?
$result = $wrap->get_active_subscribers('', 1, 1000, 'email', 'asc');
if($result->was_successful()) {
    $sum = 0.0;
    foreach ($result->response->Results as $sub) {
        foreach ($sub->CustomFields as $cf) {
            if ($cf->Key == "[Offset]") {
                $sum += floatval($cf->Value);
            }
        }
    }
    echo $english_format_number = number_format($sum);
}
?>
terryt terryt, 2 years ago

Hi Andrew,

$result = $wrap->get_active_subscribers('', 1, 1000, 'email', 'asc');

The second parameter here, which is 1, is the page number. All you need to do is go to page 2 and so on to get the next batch of 1000. :)


Terry Tice
Campaign Monitor
roomfive, 2 years ago

Thanks Terry. So if I have between 1,000 and 2,000 subscribers I'd change it to:

$result = $wrap->get_active_subscribers('', 2, 1000, 'email', 'asc');

...And if I had between 12,000 and 13,000 subscribers I'd change it to 13...?

$result = $wrap->get_active_subscribers('', 13, 1000, 'email', 'asc');
terryt terryt, 2 years ago

Hi Andrew,

To be clear, that second parameter is the page number. So, to pull the first 1,000 subscribers you would pass in 1. To pull the next 1,000 subscribers you pass in 2, and so on. We'll only ever give you 1,000 subscribers at a time so you just put this into a loop.


Terry Tice
Campaign Monitor
roomfive, 2 years ago

...So it's not possible to display the total for ALL subscribers in the whole list?

Or I suppose I could do something insane like pull in the total for each page number, 1,000 at a time, then add them all together with a PHP calculation, or something...?

terryt terryt, 2 years ago

Hi Andrew,

This is correct, you cannot pull more than 1,000 at a time. You'll need to do some calculations in your PHP code.


Terry Tice
Campaign Monitor
roomfive, 2 years ago

Okay, thanks Terry, working on that now. Thanks for your help.

Join 200,000 companies around the world that use Campaign Monitor to run email marketing campaigns that deliver results for their business.

Get started for free
1-888-533-8098