Solved

PHP for loop not executing 2nd of 2 iterations

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

832 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