Today’s tutorial is courtesy of Dave Mackey.
If you’re wondering who Dave Mackey is, he’s the owner and operator of DaveMackey.net, CCBUserGroup.com, and few CCB Plugins.
You’ve likely also run across Dave leaving comments on CCB tutorials.
Nevertheless, Dave Mackey asked the following question to CCB Tribe members:
“We can grab the maximum attendance for a group using the event_profile service’s registration.limit value.
We can calculate the current attendance at an event by using the attendance_profile service’s attendees result (by iterating through the individual attendee records within attendees and counting as we go), but this is a lot of overhead.
Is anyone aware of a way to retrieve via the CCB API the number of remaining spots available for a given event without this sort of rigmarole?”
Knowing a little bit about the attendance_profile API service, Dave’s questioning got me thinking whether or not is was possible.
It is possible, and I’ll share with you how in today’s tutorial.
Setup includes, comments and variables
This tutorial is quite simple, being very much like previous tutorials.
Start by opening a text editor of your choice, and commenting and including the necessary files.
Next, define the attendance_profile API service and the respective event id variables. Be sure to set the event id to the id of the event you’re after.
<?PHP // contains resusable globals and functions include("includes/config.php"); $apiService = 'attendance_profile'; // CCB api service $eventID = 100; ?>
Binding variables and making the API Call
To make the api call to retrieve a specific event’s attendance profile information, you’ll need define the urlData variable, setting it equal to the array to bind the following labels to their respective variables:
- srv=>apiService variable
- id=>eventID variable
Next, define the rss variable and set is equal to the ccbDetails function, passing it get as the first argument and urlData as the second argument.
<?PHP $urlData = array_filter( array( 'srv' => "$apiService", 'id' => "$eventID" ) ); $rss = ccbDetails('get',$urlData); // transform to XML ?>
Parsing the API XML response
We must set an expression to parse the total number of attendees. Here’s where things get interesting.
Normally, I would instruct you to use the following xpath query expression: //events/event. It’s a valid query, but it makes for a few more lines of unnecessary code.
Remember that we’re not searching for multiple events, but we’re in search of a single event.
That said, you can simply define a nodes variables and set it to use the following xpath query expression to retrieve the attendees info you’re in search of: //attendees/attendee.
<?PHP $nodes = $rss->xpath('//attendees/attendee'); // xpath for attendees->attendee ?>
Next, define an empty response_object variable.
Here’s where Dave is likely to smile quite wide. Instead of iterating through each individual attendee records within the attendees and counting as you go, simply define a response variable and set it to use a built-in php function to count the number of attendees: sizeof.
Since the nodes variable is a parsed xml array, using sizeof, passing it the nodes variable, to determine the number of attendees is all the heavy lifting one needs.
Finally, echo the response_object variable and you’ve have the total number of attendees without expensive and bulky iteration overhead.
<?PHP $response_object = ''; $response_object = sizeof($nodes); echo $response_object; ?>
Are you ready to flex your code?
Here’s what your comprehensive code should look like:
<?PHP // contains resusable globals and functions include("includes/config.php"); $apiService = 'attendance_profile'; // CCB api service $eventID = 100; $urlData = array_filter( array( 'srv' => "$apiService", 'id' => "$eventID" ) ); $rss = ccbDetails('get',$urlData); // transform to XML $nodes = $rss->xpath('//attendees/attendee'); // xpath for attendees->attendee $response_object = ''; $response_object = sizeof($nodes); echo $response_object; ?>
Yes, I know. We saved quite a bit of time and lines of code, aye? 😀
Okay, save your file and it’s time to flex your technical prowess.
Open your file in a web browser and you now know how to retrieve the total attendees for your event.
With a bit of elbow grease, you can also integrate the event_profiles API call with this tutorial to add a level of flexibility and dynamic data retrieval.
That’s it for this tutorial. See you back here next week. Cheers!