Controlling list order using cc_class.php

Participant

Controlling list order using cc_class.php

Hi,


I've been able to control what lists get displayed using the $contact_lists variable in cc_class.php, but I am unable to control the order in which the lists occur. I went back to my Contacts and re-ordered my lists in the way I want them displayed, however, it doesn't seem to have any affect when I select the lists through the cc_class.php API.


Any suggestions?


Steve

6 REPLIES 6
Employee

The PHP sample form that we have lists your contact lists in the order they are returned from doing an http get request to https://api.constantcontact.com/ws/customers/{user-name}/lists. This returns each contact list in the order of their list id, which is the order they are created.





We do allow you to set a <SortOrder> for each contact list in your account with a value of 1-99 (1 having the highest priority, 99 having the lowest). This item is what determines the order that the lists wil appear in your Constant Contact account as well as on your Join My Mailing List form, which is the default form you can access from your account by logging in and clicking 'Contacts > Join My Mailing List'. Unfortunately this does not impact which order they are returned through the API, thus having no impact on how they are sorted using our PHP sample. At this point we do not have the functinality implemented in our PHP sample to sort your contact lists based off of the <SortOrder>, but this may be something we add to our sample form in the future.





At this time the only suggestion I can make would be to change the 'getLists' function on cc_class.php to also pull the <SortOrder> node into the array storing the list id and list title. You could then sort the array by that field before displaying it on any of your sign up pages (ie: add_contact.php, simple_form.php etc). This would force those pages to display the contact lists based off the <SortOrder> just as the Join My Mailing List form does.


 


I hope this information helps. Please let us know if you have any other questions.

 

David J

Can you demonstrate code to alphabetize the list?

Using a function I found here, the code below should sort your lists in alphabetical order. I hope this helps, please let us know if you have any other questions.

 

 

<?php

// Instantiate Contact class
include_once('cc_class.php');
$List = new CC_List();

// Get lists in your account
$lists = $List->getLists();

// Function to sort by sub key
function sksort(&$array, $subkey="title", $sort_ascending=true) {

    if (count($array))
        $temp_array[key($array)] = array_shift($array);

    foreach($array as $key => $val){
        $offset = 0;
        $found = false;
        foreach($temp_array as $tmp_key => $tmp_val)
        {
            if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey]))
            {
                $temp_array = array_merge(    (array)array_slice($temp_array,0,$offset),
                                            array($key => $val),
                                            array_slice($temp_array,$offset)
                                          );
                $found = true;
            }
            $offset++;
        }
        if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
    }
    if ($sort_ascending) $array = array_reverse($temp_array);
    else $array = $temp_array;
}

// Run subkey sort function on your lists
sksort($lists);

// print results
print_r($lists);

David J

When i try to pull the lists using the function above, the api doesnt return anything.

I see this post is a bit old, so is there a way to sort them by the order id using the API?

 

Thanks

 

 

Daniel Mizrahi

Hello Daniel,

 

This code is actually based on a much older PHP wrapper that has been deprecated for quite some time. In addition, it is also basd on our V1 API, so it would require a V1 API key to function.

 

Unfortunately, with the API the lists are returned based on IDs and the order they were created rather than the assigned order number. In order to sort the lists, it would make the most sense to use one of our current wrappers for V2 to get all of the lists from the API, and then put the results through a sort.

 

Here is an example code to demonstrate using the V2 PHP Wrapper:

<?php
require 'src/Ctct/autoload.php';

use Ctct\ConstantContact;
use Ctct\Services;
use Ctct\Components\EmailMarketing\Campaign;
$ConstantContact = new ConstantContact("xxxxxxx"); // Enter a V2 API key.
$acc_token="xxxxxxx"; // Fill in an access token for use with the V2 API key above. You can use I/O Docs to generate one here: https://constantcontact.mashery.com/io-docs

//Get the Lists
$lists = $ConstantContact->getLists($acc_token);
//Define an alphabetical sort based on the name property of each list. This sort is case-insensitive
usort($lists, function($a, $b) {
return strcmp(strtolower($a->name),strtolower($b->name));
});
//Dump the output so we can see it! echo var_dump($lists); ?>

 

Please let me know if you have any more questions and I will be happy to provide some guidance.

 

Sincerely,

Elijah G.

API Support Specialist

Elijah G.
API Support Engineer

Excellent! Thanks for your help.

It works like a charm.

 

Daniel

Developer Portal

View API documentation, code samples, get your API key.

Visit Page