Accessing User Defined Fields Via Individual CCB Profile

It’s been a while since I’ve had a chance to post tutorial.  Please accept my sincerest apologies for having kept you waiting and anticipating the next tutorial.

Thanking God for a good number of projects, I’ve been quite busy consulting and helping a number of churches and companies with various software and application development projects.

Nevertheless, I received a request via comments about how to access user defined fields when using the CCB to return an individual profile (see below).

How to backup your CCB account

I won’t cover how to execute the the API call to retrieve or search for an individual CCB API call (see below).

Creating a reusable function for CCB API calls

Feel free to use the CCB API call instead of the hardcoded XML response I’ll use in this tutorial today. Let’s get started.

In lieu of making the CCB API to return an individual CCB profile, I’m creating my own XML content that resembles the XML Response return when executing CCB API call.

To start this tutorial, create a xmlContent variable with the following XML string content as shown below:

<?PHP

$xmlContent = <<<XML
	<response>
		<individuals count="4">
			<individual id="55">
			<first_name>Alvin</first_name>
        	<last_name>Brown</last_name>
       		<middle_name></middle_name>
        	<legal_first_name></legal_first_name>
       		<full_name>Alvin Brown</full_name>
          <user_defined_pulldown_fields count="2">
            <user_defined_pulldown_field id="1">
              <label>Follow Up</label>
            </user_defined_pulldown_field>
            <user_defined_pulldown_field id="2">
              <label>Follow Up</label>
            </user_defined_pulldown_field>
          </user_defined_pulldown_fields>
			</individual>			
		</individuals>
	</response>
XML;

?>

For more information on creating and parsing XML strings using PHP and SimpleXML, click here.

Once the xmlContent variable has been created with XML string content, define a xml variable.

Set the value of the xml variable to instantiate a new SimpleXMLElement object, passing it the xmlContent variable.

<?PHP

$xml = new SimpleXMLElement($xmlContent);

?>

The following section will appear quite familiar due to it being in most of the tutorials created.

Now we’re ready to parse the XML response example using PHP’s built-in XPATH query method.

Define a nodes variable and set it’s value to the xml variable XPATH query expression of the following: //individuals/individual.

<?PHP

// xpath for individuals->individual
$nodes = $xml->xpath('//individuals/individual');  

?>

Now it’s time to iterate the XML response for each individual record. In this example, there is only one individual record.

However, you could have multiple individual records based on the type of CCB API call executed.

To iterate through each individual record, create a foreach statement, passing it the nodes variable as node variable.

Within the foreach statement, define a procdata variable. This variable is used to validate the existence of user defined pulldown fields.

Set the procdata variable to the following:

<?PHP

foreach ($nodes as $node){

    $procdata = $node->user_defined_pulldown_fields->user_defined_pulldown_field;

}

?>

Now, create an if statement using the procdata variable. Do this to ensure that user defined pulldown fields exist before continue to access user defined pulldown fields.

<?PHP

foreach ($nodes as $node){

    $procdata = $node->user_defined_pulldown_fields->user_defined_pulldown_field;
if($procdata){

}

}

?>

Within the if statement, create another foreach statement, passing to it procdata as data variable, to iterate through each user defined pulldown fields.

<?PHP

foreach ($nodes as $node){

    $procdata = $node->user_defined_pulldown_fields->user_defined_pulldown_field;
if($procdata){

    foreach($procdata as $data){

}

}

}

?>

Within this second foreach statement, you’re now ready to echo each respective user defined pulldown field’s label (as shown below).

<?PHP

foreach ($nodes as $node){

    $procdata = $node->user_defined_pulldown_fields->user_defined_pulldown_field;
if($procdata){

    foreach($procdata as $data){
    	echo $data->label.'<br/>';
    }
}

}

?>

Close the foreach, if and foreach statements, and you’re ready to piece everything together for a quick code test.

Below is what your code should look like:

<?PHP

$xmlContent = <<<XML
	<response>
		<individuals count="4">
			<individual id="55">
			<first_name>Alvin</first_name>
        	<last_name>Brown</last_name>
       		<middle_name></middle_name>
        	<legal_first_name></legal_first_name>
       		<full_name>Alvin Brown</full_name>
          <user_defined_pulldown_fields count="2">
            <user_defined_pulldown_field id="1">
              <label>Follow Up</label>
            </user_defined_pulldown_field>
            <user_defined_pulldown_field id="2">
              <label>Follow Up</label>
            </user_defined_pulldown_field>
          </user_defined_pulldown_fields>
			</individual>			
		</individuals>
	</response>
XML;

$xml = new SimpleXMLElement($xmlContent);


// xpath for individuals->individual
$nodes = $xml->xpath('//individuals/individual');  

foreach ($nodes as $node)
{

    $procdata = $node->user_defined_pulldown_fields->user_defined_pulldown_field;
if($procdata){

    foreach($procdata as $data){
    	echo $data->label.'<br/>';
    }
}

}

?>

Remember, you can swap the static html code and use a dynamic CCB API call instead (recommended).

Save your file and test the results via a web browser.

Happy coding and development, and that’s all for today. See you next week.

Related Posts

Subscribe and receive the following...

  • Inside CCB tips and tricks
  • Instant CCB tutorial alerts and updates
  • CCB How To's, Videos, Webinars and more...