We’re back with another Church Community Builder (CCB) tutorial to further your CCB API knowledge and usage one step further.
This week, we’ll review the previous tutorial that focused on how to retrieve a full list of process managers and their total number of processes managed (see image below).
We’ll resume working to expound upon the codebase that was established in the previous tutorial using CCB’s API Documentation.
You’ll have to read the previous tutorial if you’re reading this for the first time. I won’t cover any of the previous details or step-by-step setup.
Our aim today is set upon listing each process manager, and listing each of their respective processes by name.
So, our finished product will look similar to the picture above, yet it’ll show each process name in a bulleted list below each person’s name. Let’s get started.
Start with Previous Codebase
Opening an a text editor of your choice, create a php file, naming and saving it as follows: process-managers-process-names.php.
Next, copy the final code from the previous tutorial in your newly create php file.
<?PHP /** * Process Managers List */ // contains resusable globals and functions include("includes/config.php"); $apiService = 'process_managers'; // CCB api service //$personId = 3224; // specific person's id $urlData = array_filter( array( 'srv' => "$apiService" //,'id' => $personId ) ); $rss = ccbDetails('get',$urlData); // transform to XML $nodes = $rss->xpath('//managers/manager'); // xpath for resources $response_object = ''; foreach ($nodes as $node) { $response_object .= $node['id'].' - '.$node->name.' (Number of Processes Managed: '.sizeof($node->processes->process).')<br>'; /* $node->processes // collective grouping of processes $node->processes->process // each individual process name */ } echo $response_object; ?>
Listing Process Names for Each Process
Now that you have the previous tutorial’s codebase in its entirety, it’s time to make a few modifications to the initial foreach statement of the code.
One of the first changes to make is to create a processes variable and set it equal to the following value: $node->processes->process.
You’ll notice that this is the same value that is found within the PHP built-in sizeof method used to total the number of processes for each person.
Replace the code within the sizeof method with the processes variable as shown below.
<?PHP foreach ($nodes as $node) { $processes = $node->processes->process; $response_object .= $node['id'].' - '.$node->name.' (Number of Processes Managed: '.sizeof($processes).')<br>'; /* $node->processes // collective grouping of processes $node->processes->process // each individual process name */ } ?>
Next, we’ll want to create a bulleted list of the process names for each person.
To do so, redefine two response_object variables. The first response_object variable should be set equal to the open ul html tag while the second response_object variable should be set equal to the close ul html tag.
<?PHP foreach ($nodes as $node) { $processes = $node->processes->process; $response_object .= $node['id'].' - '.$node->name.' (Number of Processes Managed: '.sizeof($processes).')<br>'; $response_object .= '<ul>'; $response_object .= '</ul>'; /* $node->processes // collective grouping of processes $node->processes->process // each individual process name */ } ?>
Now that we have the open and close ul html tags for the bulleted list, it’s time retrieve and display the names of each process.
To do so, create a foreach statement between the newly create response_object variables, and have it iterate the processes variable as the array_expression which is an array of the process names.
Within the foreach statement, redefine the response_object variable and set it equal to the processData variable that was set as the value of the foreach statement.
Be sure to concatenate the processData variable between the open and close li html tags for the bulleted list.
<?PHP foreach ($nodes as $node) { $processes = $node->processes->process; $response_object .= $node['id'].' - '.$node->name.' (Number of Processes Managed: '.sizeof($processes).')<br>'; $response_object .= '<ul>'; foreach($processes as $processData){ $response_object .= '<li>'.$processData.'</li>'; } $response_object .= '</ul>'; /* $node->processes // collective grouping of processes $node->processes->process // each individual process name */ } ?>
Time to test your technical prowess
Believe it or not, that’s all there is to this tutorial. Your code should look like the code below. You’re now ready to save and open the file using a web browser:
<?PHP /** * Process Managers List and Their Respective Process Names */ // contains resusable globals and functions include("includes/config.php"); $apiService = 'process_managers'; // CCB api service //$personId = 3224; // specific person's id $urlData = array_filter( array( 'srv' => "$apiService" //,'id' => $personId ) ); $rss = ccbDetails('get',$urlData); // transform to XML $nodes = $rss->xpath('//managers/manager'); // xpath for resources $response_object = ''; foreach ($nodes as $node) { $processes = $node->processes->process; $response_object .= $node['id'].' - '.$node->name.' (Number of Processes Managed: '.sizeof($processes).')<br>'; $response_object .= '<ul>'; foreach($processes as $processData){ $response_object .= '<li>'.$processData.'</li>'; } $response_object .= '</ul>'; /* $node->processes // collective grouping of processes $node->processes->process // each individual process name */ } echo $response_object; ?>
When successfully executed, the following image should appear with your respective process managers and their bulleted list of respective process names.
Although we display a list of process managers and now a bulleted list of their respective process names, you could easily turn this script into a jump menu using jQuery or Javascript.
When I reference jump menu, think of selecting a user by name in a drop down menu. Once the use is selected, then another drop down menu appears with that users respective process names in it.
We completed a tutorial that resembles such functionality once before (hint, hint). Review the tutorial and see if you can take it to the next level. 😉
See you back here next week… That’s all for now.