Jump to content

Content Disappearing From $Output During Loop


jcanker

Recommended Posts

I'm working on some code for my son's cub scout pack to track the boy's progress toward their rank requirements.

I've successfully gotten the code to build an array from the database to show the relationships between requirements & subrequirements.

 

As a next step, I want to step through the array and display them in a user-friendly manner. The top-level requirements (elements-numbered 1, 2, 3, etc) are added to the list fine. As it steps through the child requirements (labeled as a,b,c, etc), they seem to be added to $output as expected and appear as part of $output when echoed to the screen. However, when the foreach loop goes through another iteration, all the child-level requirements are dropped off from $output rather than have the new top-level appended to the output.

 

My suspicion is that it has something to do with scope, but I can't understand why it says $output = 1,2,3,4,5,6,7abc then suddenly it says $output = 1,2,3,4,5,6,7,8. What am I missing?

 

Sample echo output that I created for testing shows (I edited the output to show requirement 7a,b,&c, which is the first spot where this scenario occurs because the earlier requirements don't have children:

The temporary output build at this point is:

c -- Explain and agree to follow the Outdoor Code

 

The current output at this point is:

 

Do these

1 -- Have an adult member of your family read the Weblos Scout Parent Guide (pages 1-22) and sign here

2 -- Be an active member of your Webelos den for three months (Active) means having good attendance, payong your den dues, and working on den projects.)

3 -- Know and explain the meaning of the Webelos badge

4 -- Point out the trhee special parts of the Webelos Scout unifrm. Tell when to wear the uniform and when not to wear it.

5 -- Earn the Fitness and Citizen activity badges and one other ativity badge from a different activity group

6 -- Plan and lead a flag ceremony in your den that includes the U.S. flag.

7 -- Show that you know and understand the requirements to be a Boy Scout

a -- Demonstrate the Scout sale, Scout sign, and Scout handshake. Explain when you would use them.

b -- Explain the Scout Oath, Scout Law, Scout motto, and Scout slogan.

c -- Explain and agree to follow the Outdoor Code

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

We're starting a new run through the foreach loop and as we start it, output is set to:

 

 

Do these

1 -- Have an adult member of your family read the Weblos Scout Parent Guide (pages 1-22) and sign here

2 -- Be an active member of your Webelos den for three months (Active) means having good attendance, payong your den dues, and working on den projects.)

3 -- Know and explain the meaning of the Webelos badge

4 -- Point out the trhee special parts of the Webelos Scout unifrm. Tell when to wear the uniform and when not to wear it.

5 -- Earn the Fitness and Citizen activity badges and one other ativity badge from a different activity group

6 -- Plan and lead a flag ceremony in your den that includes the U.S. flag.

7 -- Show that you know and understand the requirements to be a Boy Scout

 

 

And the thisOutput is set to:

7 -- Show that you know and understand the requirements to be a Boy Scout

 

---/////---/////---/////---/////---/////---/////---/////---/////---/////---/////---/////---/////

 

The temporary output build at this point is:

 

8 -- Faith

 

 

And the relevant code is as follows:

//initialize output
$output = "<div>";
//step through the array and create OL/LI where appropriate
//$thisArray = $testArray;
function listRequirements($thisArray, $output)
{
echo "<br/>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br/>";
echo "<br/>We're starting listRequirements and as we start it, output is set to:<br/>".$output."<br/>";
foreach($thisArray as $key => $value)
{
echo "<br/>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br/>";
echo "<br/>We're starting a new run through the foreach loop and as we start it, output is set to:<br/>".$output."<br/>";
echo "<br/><br/>And the thisOutput is set to: ".$thisOutput."<br/>";

$thisOutput = "";
//see if this requirement has a name and include it if so
if(array_key_exists("reqName", $thisArray[$key]))
{
$thisOutput .= "<div>".$thisArray[$key]['reqName'];
$closeDiv = 1;
}
if(array_key_exists("reqText", $thisArray[$key]))
{
if(isset($closeDiv) && $closeDiv == 1) {$thisOutput .= " -- ".$thisArray[$key]['reqText'];}
else{
$thisOutput .= "<div>".$thisArray[$key]['reqText'];
$closeDiv = 1;
}
}
if(isset($closeDiv) && $closeDiv == 1) {$thisOutput .= "</div>";}
$closeDiv = 0;

echo "<br/>---/////---/////---/////---/////---/////---/////---/////---/////---/////---/////---/////---/////<br/>";
echo "<br/>The temporary output build at this point is: <br/>".$thisOutput;
$output .= $thisOutput;
//echo "<br/>..............................................................<br/>";
echo "<br/>The current output at this point is: ".$output."<br/>";
//see if there are any children for this element
if(array_key_exists("children", $thisArray[$key]))
{
echo "<br/>=================================================<br/>";
echo "<br/>We're entering the children loop, and we're passing the output: ".$output."<br/>";
$result = listRequirements($thisArray[$key]['children'], $output);
}
}//end foreach ($testARray as $key=> $value)
//echo "<br/>After going through all the keys, the output is:<br/>".$output;
// return $output;
}// end function listRequirements($thisArray)
$result = listRequirements($testArray, $output);

 

 

 

 

 

 

For the record, the array that it's pulling from is:

 

Array

(

[1] => Array

(

[reqText] => Do these

[children] => Array

(

[2] => Array

(

[reqName] => 1

[reqText] => Have an adult member of your family read the Weblos Scout Parent Guide (pages 1-22) and sign here

)

 

[3] => Array

(

[reqName] => 2

[reqText] => Be an active member of your Webelos den for three months (Active) means having good attendance, payong your den dues, and working on den projects.)

)

 

[4] => Array

(

[reqName] => 3

[reqText] => Know and explain the meaning of the Webelos badge

)

 

[5] => Array

(

[reqName] => 4

[reqText] => Point out the trhee special parts of the Webelos Scout unifrm. Tell when to wear the uniform and when not to wear it.

)

 

[6] => Array

(

[reqName] => 5

[reqText] => Earn the Fitness and Citizen activity badges and one other ativity badge from a different activity group

[children] => Array

(

[7] => Array

(

[reqAwardID] => 2

)

 

[8] => Array

(

[reqAwardID] => 3

)

 

[9] => Array

(

[awardNum] => 1

[awardGroupID] => Array

(

[0] => 2

[1] => 4

[2] => 5

)

 

)

 

)

 

)

 

[10] => Array

(

[reqName] => 6

[reqText] => Plan and lead a flag ceremony in your den that includes the U.S. flag.

)

 

[11] => Array

(

[reqName] => 7

[reqText] => Show that you know and understand the requirements to be a Boy Scout

[children] => Array

(

[12] => Array

(

[reqName] => a

[reqText] => Demonstrate the Scout sale, Scout sign, and Scout handshake. Explain when you would use them.

)

 

[13] => Array

(

[reqName] => b

[reqText] => Explain the Scout Oath, Scout Law, Scout motto, and Scout slogan.

)

 

[14] => Array

(

[reqName] => c

[reqText] => Explain and agree to follow the Outdoor Code

)

 

)

 

)

 

[15] => Array

(

[reqName] => 8

[reqText] => Faith

[children] => Array

(

[16] => Array

(

[reqText] => After completing the rest of requirement 8, do these (a,b, and c)

[children] => Array

(

[17] => Array

(

[reqName] => a

[reqText] => Know: Tell what yo have learned about faith

)

 

[18] => Array

(

[reqName] => b

[reqText] => Commit: Tell how these faith experiences help you live your duty to God. name one faith ractice you will continue to do in the future.

)

 

[19] => Array

(

[reqName] => c

[reqText] => Practice: After doing these equirements, tell what you have learned about your beliefs

)

 

)

 

)

 

[20] => Array

(

[qtyCompReq] => 1

[reqText] => And do one of these (d OR e)

[children] => Array

(

[21] => Array

(

[reqName] => d

[reqText] => Earn the religios emblem of your faith

[footnote] => Footnote text

[footnoteMarker] => *

)

 

[22] => Array

(

[reqName] => e

[qtyCompReq] => 2

[reqText] => Do two of these:

[children] => Array

(

[23] => Array

(

[reqName] => *

[reqText] => Attend the mosque, church, synagogue, temple, or other religious organization of your choice, talk with your religious leader aboutyour beliefs. Tell your family and your Webelos den leader what you learned.

)

 

[24] => Array

(

[reqName] => *

[reqText] => Discuss with your family and Webelos den leader how your religious beliefs fit in with the cout Oath and Scout Law, and what character-building traits your religious beliefs hve in common with the Scout Oath and Scout Law.

)

 

[25] => Array

(

[reqName] => *

[reqText] => With your religious leader, disuss and make a plan to do two things you think will help you draw nearer to God. Do these things for amonth.

)

 

[26] => Array

(

[reqName] => *

[reqText] => For at least a month, pray or meditate reverently each day as taught by your family, and by your church, temple, mosque, synagogue ,or religious group.

)

 

[27] => Array

(

[reqName] => *

[reqText] => Underthe direction ofyour religious leader, do an act of service for someone else. Talk about your service with your family and Webelos den leader. Tell them how it made you feel.

)

 

[28] => Array

(

[reqName] => *

[reqText] => List at least two ways you blieve you have lived according to your religious beliefs

)

 

)

 

)

 

)

 

)

 

)

 

)

 

)

 

)

 

)

Link to comment
Share on other sites

For the record for anyone who sees this post-humously, my suspicions about scope were correct. Changing references of $output to a session variable, $_SESSION['output'] solved the problem.

 

It's been a long night and I'm probably missing something, but I'm still not certain why scope affecting it when $output is initialized outside the function and passed to it, even when it's called recursively to get to the child elements.

Link to comment
Share on other sites

The issue is that you're not saving the output you generate in each child loop back to the $output variable. In this part here:

if(array_key_exists("children", $thisArray[$key]))
{
   $result = listRequirements($thisArray[$key]['children'], $output);
}

 

You are passing the current value of $output to the child loop. In that child loop you append the new a, b, and c requirements to $output. Once that instance of the function returns though, $output goes back to how it was before.

 

Each instance of your function gets it's own copy of the $output variable. Whatever it does to it is isolated to just that particular instance of the function. Any changes made will not affect the other instances' value. There are two ways to solve this problem.

 

1) Have your function return the new value of $output, and then save that value in the parent instance:

if(array_key_exists("children", $thisArray[$key]))
{
   $output = listRequirements($thisArray[$key]['children'], $output);
}

and then at the end of the function you would add return $output; (you have it but it's commented out)

 

-or-

2) Make $output a pass-by-reference parameter so that changes made to it inside the function are visible from outside the function as well. To do that you're function definition would change to:

function listRequirements($thisArray, &$output)

 

Then when you call it you would just do:

if(array_key_exists("children", $thisArray[$key]))
{
   listRequirements($thisArray[$key]['children'], $output);
}

 

and the initial call would be:

listRequirements($testArray, $output);
echo $output;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.