Solved

PHP for loop not executing 2nd of 2 iterations

Posted on 2011-09-10
5
352 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 75

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 75

Expert Comment

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

820 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