Solved

PHP for loop not executing 2nd of 2 iterations

Posted on 2011-09-10
5
349 Views
Last Modified: 2012-05-12
Can't seem to get the second array element to print out...please help!
There are two campers, each with a complete set of information, in the array
$displayCampersOfParent

The current display only shows the first camper's information, and then the for loop terminates...
<?php
		// insert short blocks for parent, all campers
		/*echo '<pre>';
		print_r($displayCampersOfParent);
		echo '</pre>';*/
		
		// display camper name, selection of sessions
		for ($i = 0; $i < count($displayCampersOfParent); $i++){
			
			// set camper number loop
			$loop_counter = 1;
			echo $loop_counter;
			
			echo '<dl><h3>';
			echo $displayCampersOfParent[$i]['t_firstname_en'] .' ';
			echo $displayCampersOfParent[$i]['t_lastname_en'];
			echo '</h3>';
			if ($_SESSION['UserLang'] == 'JP'){
				echo '<br />';
				echo $displayCampersOfParent[$i]['t_lastname_jp'] .' ';
				echo $displayCampersOfParent[$i]['t_firstname_jp'];
			}
			echo '<br />';
			
			// display 3 session selection blocks
			for ($session_loop_counter = 1; $session_loop_counter < 4; $session_loop_counter++){
				
				echo '<dt><span class="NoStarRequired">* </span>';
				echo $CampSessionSelection;
				echo '</dt><dd>';
				echo '<select name="';
					echo 'Camper_' .$loop_counter .'_Session_' 
						.$session_loop_counter .'_List'; 
					echo '" ';
					echo 'id="';
					echo 'Camper_' .$loop_counter .'_Session_' 
						.$session_loop_counter .'_List'; 
					echo '">';
					echo '<option value="">';
					echo $CamperSessionOptions; 
					echo '</option>';
					for ($i = 0; $i < count($obj->mRegSessions); $i++){
					  
						  // build session dates
						  $date_start = date_create($obj->mRegSessions[$i]['d_startdate']);
						  $date_end = date_create($obj->mRegSessions[$i]['d_enddate']);
						  $print_date_start = date_format($date_start, 'n/d/Y');
						  if ($date_start == $date_end){
							  $print_date_end = '';
						  } else {
							  $print_date_end = date_format($date_end, 'n/d/Y');
						  }
						  
						  // output session dates
						  if ($_SESSION['UserLang'] == 'EN'){
							  $ProgramName = $obj->mRegSessions[$i]['t_program_name_en'];
							  $SessionName = $obj->mRegSessions[$i]['t_session_name_en'];
						  } else {
							  $ProgramName = $obj->mRegSessions[$i]['t_program_name_jp'];
							  $SessionName = $obj->mRegSessions[$i]['t_session_name_jp'];
						  }
							  
						  if ($SessionName == '') {
							  echo '<option value="' .$obj->mRegSessions[$i]['pk_session'] .'">'
							  .$ProgramName ."&nbsp;&nbsp;" .$print_date_start 
							  ." &mdash; " .$print_date_end .'</option>';
						  } else {
							  echo '<option value="' .$obj->mRegSessions[$i]['pk_session'] .'">'
							  .$ProgramName .', ' .$SessionName .": " .$print_date_start 
							  ." &mdash; " .$print_date_end .'</option>';
						  }
      				 } // end for loop
    			echo '</select></dd>';
			
			// end session display for loop--3 repetitions
			}
			
			
			// end this camper's session registration block
			echo '</dl>';
			
			// increment counter
			$loop_counter++;
			echo $loop_counter;
		// end for loop, after displaying all campers	
		}
     ?>

Open in new window

0
Comment
Question by:kpisor
  • 2
  • 2
5 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 36517758
You reused $i as a loop counter in line 42. You initially declared it in line 8. You are most likely overwriting the value in the 2nd loop, so by the time the first loop comes around a 2nd time, you have already exceeded the bounding condition. Try using different variables for each loop.
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 36517762
I can't see the error. What do you get when you

echo count($displayCampersOfParent);

Open in new window


Can you show the output from
   	        /*echo '<pre>';
		print_r($displayCampersOfParent);
		echo '</pre>';*/

Open in new window

(without the comments)
0
 

Author Closing Comment

by:kpisor
ID: 36518237
Kaufmed,
Fantastic. I stared at this thing for 30 minutes, and never saw the two $i variables. I put in $j and it worked immediately.
Thanks!
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 36518844
@kaufmed-

d'oh. Good pickup. I missed it too.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36518975
NP. Glad to help  = )
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Read about achieving the basic levels of HRIS security in the workplace.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
The viewer will learn how to count occurrences of each item in an array.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now