Next, define a giftingRating variable and set its value equal to 15. I choose 15 because the top 5 spiritual gifting rankings when totaled were equal to or greater than 15. Feel free to change this value to your own liking. Again, I prefer the top 5 spiritual gifts to be updated in CCB for the respective person’s profile.

Last but not least, define a personID variable and set it equal to a person’s CCB ID. For the sake of this example, I’ve chosen to hardcode the value. You could easily run a person lookup to retrieve the person’s id from CCB with a little extra elbow grease.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?PHP

// contains resusable globals and functions

include("includes/config.php");

$file=clean_input($_REQUEST['file']);

if(!$file||!file_exists($file))

header("Location: index.php");

$giftingRating=15;// we only want to update a person's rating if they score 15 or higher. logic found in function getPersonSGAFitArray

$personID=3224;// hardcoded but you could easily automate a lookup of some sort

?>

Retrieve the person’s current fit selections

Before updating a person’s respective fit selections with new selections, ensure you retrieve and store the current selections.

If not, you’ll overwrite a person’s current fit selections simply updating their fit with new fit or spiritual gifting selections.

To retrieve the person’s current fit selections, define a personFitArray variable and set it equal to the getPersonFitArray function, passing it the personID variable as the function argument.

The function simply returns the fit or spiritual gifting name and its respective id as a key-value pair array.

Retrieve the person’s top 5 spiritual gifts

To retrieve a person’s top 5 spiritual gifts, define a personSGAFitArray variable and set it equal to the getPersonSGAFitArray function, passing it the file variable as the function argument.

This function simply reads the actual csv file and returns an array of only the spiritual gifts with a total value greater than or equal to the giftingRating variable defined at the beginning of this tutorial.

1

2

3

4

5

<?PHP

$personSGAFitArray=getPersonSGAFitArray($file);// returns string value array of the top 5 giftings

?>

Retrieve the default list of spiritual gifts

Since we don’t have the id of each spiritual gift return as an array stored as the personSGAFitArray variable, then we must retrieve the default list of spiritual gifts with their respective ids.

To retrieve this list, define a giftingListingArray variable and set it equal to the getGiftingList function.

Time for a bit of tricky PHP heavy lifting and logic

We’re about to become if, if else, and foreach nested happy in the following lines of code in this section. It’ll be a bit more complicated than in previous weeks, but hang in there. You’ll make it! 🙂

Before we move too far into the heavy lifting, define the follow two variables as empty arrays:

newGiftingArray variable will contain list that will be updated in CCB profile

lastGifting variable is the last known gifting used to keep us from having duplicate gifts captured in the newGiftingArray variable

The first step in this heavy lifting logic and process is to parse each of the array values stored in the personSGAFitArray, also known as the top 5 spiritual gifts for a person. To do this, execute a foreach statement as shown below.

1

2

3

4

5

6

7

8

9

10

<?PHP

$newGiftingArray=array();

$lastGifting=array();

foreach($personSGAFitArrayas$newGifting){

}

?>

Within this foreach statement, find the integer id value of each spiritual gift’s string value. To do this, execute an additional foreach statement parsing the giftingListArray variable’s array value into key=>pair values as shown below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<?PHP

$newGiftingArray=array();

$lastGifting=array();

foreach($personSGAFitArrayas$newGifting){

foreach($giftingListArrayas$key=>$data){

}

}

?>

Within the giftingListArray foreach statement, create an if statement for when the default gift list string is equal to that of the respective top 5 gifting of the person, meaning the gifting has been matched or found.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?PHP

$newGiftingArray=array();

$lastGifting=array();

foreach($personSGAFitArrayas$newGifting){

foreach($giftingListArrayas$key=>$data){

if($data[1]==$newGifting){

// The gifting has been found

}

}

}

?>

Within this if statement, execute an if else statement to ensure there’s a defined value for the personFitArray variable.

If a value is defined, then we know that a person has current fit or spiritual gift selections checked in CCB. If a value is not defined, then we know that person did not have current fit or spiritual gift selections checked in CCB.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<?PHP

$newGiftingArray=array();

$lastGifting=array();

foreach($personSGAFitArrayas$newGifting){

foreach($giftingListArrayas$key=>$data){

if($data[1]==$newGifting){

// The gifting has been found

if($personFitArray){

}else{

}

}

}

}

?>

Within this if statement, execute a foreach statement that parses each personFitArray array values as key=>value pairs.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<?PHP

$newGiftingArray=array();

$lastGifting=array();

foreach($personSGAFitArrayas$newGifting){

foreach($giftingListArrayas$key=>$data){

if($data[1]==$newGifting){

// The gifting has been found

if($personFitArray){

foreach($personFitArrayas$key=>$value){

}

}else{

}

}

}

}

?>

Within the foreach statement, execute an if else statement used to rule out duplicates. There is nothing more to do within the if of this if else statement. In the else section of the if else statement, execute another if statement to ensure the gifting currently checked is not in the lastGifting array. This simply rules out duplicates. If the new gifting is not found in the lastGifting array, then add the new gifting string and respective id to the personFitArray array.

// we don't want duplicates so we have to set a last known array of used giftings.

// if the gifiting is not found in the last known array of used giftings,

// then add the new gifting to existing giftings.

if(!in_array($newGifting,$lastGifting))

$personFitArray[]=array((integer)$data[0],(string)$newGifting);

$lastGifting[]=$newGifting;

}

}

}else{

}

}

}

}

?>

Now we’re ready to traverse the nest back up a few levels to the if statement for the personFitArray, rather write logic for the else section in the if personFitArray variable statement.

This else statement is basically written for a personFitArray not having current fit or spiritual gift selections checked in CCB initially. So, basically copy and paste the last three lines of code and paste lines into the else section of the if statement for the personFitArray. Yes, just that easy.