?
Solved

PHP for loop not executing 2nd of 2 iterations

Posted on 2011-09-10
5
Medium Priority
?
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

801 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