PHP Foreach loop issue

I'm not really sure how to ask this question but I'll give it a shot.  I have a query that returns data in a foreach loop.  I am attempting to return a piece of HTML for each iteration of the loop.
<?php
$order_query = $this->db->select('*')
			->from('_items')
			->where('order_id', $open_id)
			->where('entry_id', '29')
			->get();
foreach ($order_items as $row)
	{
 // Create Variable//
$this->page_html.='<br /><br />
		<div class="declaration">
   Blah, Blah Blah
   </div>';
}

return $this->page_html;

Open in new window

The issue I am having right now is that if there are two returns from the query, the return$this->page_html loops 4 times. If there are 3 returns it loops 6 times.
LVL 8
rgranlundAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
It look like you're using PDO or mysql. How do you fetch result? Probably, the query retuirns records in an array which is both associative and number indexed. Be sure to specify PDO::FETCH_ASSOC

$this->db->fetchAll(PDO::FETCH_ASSOC)

Open in new window


or to use

$result->fetch_assoc()

Open in new window

0
rgranlundAuthor Commented:
@marco
$order_items = $order_query->result_array();
$order_query->free_result();
0
Marco GasiFreelancerCommented:
Well, are you using Active Record? o the result is a 2d array: an array where each element is an associative array, isn't it?
Can then you post here the whole code of the function? Since I see a return $this->page_html, I guess you have posted just an excerpt of the code...
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

rgranlundAuthor Commented:
it's pretty big but that is a good idea.  It jumps through a bunch of hoops and I am probably missing something simple.

<?php
		$mid = $this->EE->db->select('member_id, email')
				->where('member_id', $member)
				->limit('1')
				->get('exp_members');
		
		$mem_id = $mid->result_array();
		
		$mid->free_result();	
		
		foreach($mem_id as $row) 	{
			$member = $row['email'];
			$member_id = $row['member_id'];
		}
		//echo '<pre>';
			//var_dump($member_id);
			//echo '</pre>';	
		if ($member)
		{
						//  START GRAB Open Order Ids
				$open_order_id = $this->db->select('order_id')
					->where('member_id', $member_id)
					->where('status', 'open')
					->get('exp_cartthrob_subscriptions');
						
				$open_orders = $open_order_id->result_array();
				
				$open_order_id->free_result();
			//echo '<pre>';
			//var_dump($open_order_id);
			//echo '</pre>';
				
				foreach($open_orders as $oid)	{
					$open_id = $oid['order_id'];
			
			$channel_query = $this->db->select('entry_id')
				->where('field_id_77', $member)
				 ->get('exp_channel_data');
			
			$channel_entries = $channel_query->result_array();
		
			$channel_query->free_result();
	//echo '<pre>';
			//var_dump($channel_query);
			//echo '</pre>';
			foreach($channel_entries as $cid)	{
				$entry_id = $cid['entry_id'];
			
			
					
		$order_query = $this->db->select('*')
			->from('exp_cartthrob_order_items')
			->where('order_id', $open_id)
			->where('entry_id', '29')
			->get();

		
		$order_items = $order_query->result_array();

		$order_query->free_result();
		
					foreach ($order_items as $row)
					{
						$extra = _unserialize($row['extra'], TRUE);
						
							if ($keep_extra)
							{
								$row['extra'] = $extra;
							}
							else
							{
								foreach ($extra as $key => $value)
								{
									if ( ! isset($row[$key]))
									{
										$row[$key] = $value;
								
									}
								}
				
								unset($row['extra']);
							}
							

							$order_id = $row['row_id'];
							$fn = $row['insured_first_name'];
							$ln = $row['insured_last_name'];
							$ma = $row['bicycle_location'];
							$ad = $row['location_address'];
							$ct = $row['location_city'];
							$st = $row['location_state'];
							$zip = $row['location_zip'];
							$bike_type = $row['type_bicycle'];
							$model = $row['model'];
							$sn = $row['serial_number'];
							$bike_val = $row['bike_value'];
							$sd = date('l F d Y', strtotime( date('l F d Y', $row['entry_date'])));
							$ed = date('l F d Y', strtotime( date('l F d Y', $row['entry_date']) . ' + 1 year'));
							$deduc = $row['deductible_option_name'];
							$product_deduc = $row['physical_damage_total'];
							$product_liability = $row['liability_total'];
							$liable = $row['liability_option_name'];
							$product_medical = $row['medical_total'];
							$medical = $row['medical_option_name'];
							$product_vehicle = $row['vehicle_total'];
							$vehicle = $row['vehicle_option_name'];
							$policy_total = $row['policy_total'];
				 			
		//  echo '<pre>';
		//  var_dump( $extra );
		//  echo '</pre>';					
			
				//  START Liability Container to be included  		
							if($liable !="") {
								$liable_cont = '<tr>
													<td>Liability</td>
													<td style="text-align:center;">'.$liable.'.00</td>
													<td style="text-align:center;"></td>
													<td style="text-align:center;">$ '.$product_liability.'.00</td>
												</tr>';	
							} else {
								$liable_cont = "";
							}
				
				//  START Medical Container to be included  			
							if($medical !="") {
								$medical_cont = '<tr>
													<td>Medical</td>
													<td style="text-align:center;">'.$medical.'.00</td>
													<td style="text-align:center;"></td>
													<td style="text-align:center;">$ '.$product_medical.'.00</td>
												</tr>';	
							} else {
								$medical_cont = "";
							}
							
				//  START Vehicle Container to be included  
							
							if($vehicle !="") {
								$vehicle_cont = '<tr>
													<td>Vehicle Contact</td>
													<td style="text-align:center;">'.$vehicle.'.00</td>
													<td style="text-align:center;"></td>
													<td style="text-align:center;">$ '.$product_vehicle.'.00</td>
												</tr>';	
							} else {
								$vehicle_cont = "";
							}
							
														
	$this->policy_html.='<br /><br />
		<div class="print-declaration">
		<a href="{site_url}images/uploads/pdf/StarrPrivacyPolicyandPractices.pdf" target="new">
		 	<button>
		 		Privacy Policy and Practices
		 	</button>
		 </a>


		 <a href="{site_url}images/uploads/pdf/StarrOFACNotice.pdf" target="new">
		 	<button>
		 		OFAC Notice
		 	</button>
		 </a>
		 
		 <a href="{site_url}images/uploads/pdf/SpokeRoadsideAssistance.pdf" target="new">
		 	<button>
		 		Roadside Assistance
		 	</button>
		 </a>
		 
		 <a href="{site_url}index.php/faq/general-questions">
		 	<button>
		 		Roadside Assistance FAQ\'s
		 	</button>
		 </a>		 
		 
		</div>
	<div class="my-contract">
		<h2>My Contract</h2>
		<div id="my_contract">
		<!--  START DECLARATION  -->
			<div id="declaration">
				
			<!--  START LOGO  -->
				<div class="declaration-logo">
					<div class="declaration-logo-img">
						<img src="{site_url}images/uploads/image-library/star_logo.jpg" alt="Star" > 
					</div>
				</div>
			<!--  END LOGO  -->
			
			<!--  START ADDRESS CONTAINER  -->	
				<div class="star-insurance-address">
					<span class="star-name">StarrCYCLE DECLARATIONS</span><br />
					<span class="lrg-black">Dallas, TX<br />
					Administrative Office: 399 Park Avenue, 8th Floor, New York, NY 10022</span>
				</div>
			<!--  END ADDRESS CONTAINER  -->
			
			
			<!--  START POLICY NUMBER  -->	
				<div class="policy-number"><span class="med-black">POLICY NUMBER: # '.$order_id.'</span> </div>
			<!--  END POLICY NUMBER  ->
			
			<!--  START PERSONAL INFO CONTAINER  -->
				<div class="personal-info-container">
					<table class="pic">
						<tr>
							<td class="pic-td"><span class="med-black">NAMED INSURED AND MAILING ADDRESS:</span></td><td class="pic-td"><span class="med-black">PRODUCER</span></td>
						</tr>
						<tr>
							<td class="pic-td-center-prime">'.$fn.' '.$ln.'<br />'
									.$ad.'<br />'
									 .$ct.' '.$st.', '.$zip.
							'</td><td class="pic-td-center">Dovetail Managing General Agency Corp.</td>
						</tr>
					</table>
				
					<table class="pic">
					 	<tr>
							<td colspan="4" class="pic-td-full"><span class="med-black">POLICY PERIOD</span></td>
						</tr>
						<tr>	
							<td width="100"><span class="title-black">FROM::</span></td><td> <span class="black">'
								.$sd.'</span></td>
							<td width="100">
								<span class="title-black">TO::</span></td><td> <span class="black">'
    							.$ed.'
    							</span>
							</td>
						</tr>
						<tr>
							<td colspan="4">At 12:01 A.M. Standard Time At Your Mailing Address Shown Above.</td>
						</tr>
					</table>	
					
					<table class="pic">
						<tr>
							<td colspan="4" class="pic-td-full"><span class="med-black">Your Insured Bicycles</span></td>
						</tr>
						<tr>
							<td><span class="title-black">Item#</span></td>
							<td><span class="title-black">Description</span></td>
							<td><span class="title-black">Serial Number</span></td>
							<td><span class="title-black">Limit</span></td>
						</tr>
					 	<tr>
							<td>'.$order_id.'</td>
							<td>'.$bike_type.' / '.$model.'</td>
							<td>'.$sn.'</td>
							<td>'.$deduc.'</td>
						</tr>
					</table>
					
					<table class="pic">
						<tr>
							<td class="pic-td-full"><span class="med-black">INSURED LOCATION</span></td>
						</tr>
						<tr>
							<td>' 
							.$ma .' | '.$ad.' '.$ct. ' ' .$st. ' ' .$zip.'
							</td>
						</tr>
					</table>
					
					<table class="pic">
						<tr>
							<td class="pic-td-full"><span class="med-black">SCHEDULE OF COVERAGE</span><br />
							<span class="black">This policy only provides those coverages where a specific charge is shown in the premium column below or which is indicated as included without a specific charge either below or in your policy. Coverage will only apply to those bicycles shown as "Your Insured Bicycles" above. </span>
							</td>
						</tr>
					</table>
				
					
					<table class="pic">
						<tr>
							<td width="25%" style="text-align:center;"><span class="title-black">Coverage</span></td>
							<td style="text-align:center;"><span class="title-black">Limit</span></td>
							<td style="text-align:center;"><span class="title-black">Deductible</span></td>
							<td style="text-align:center;"><span class="title-black">Premium</span></td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
						</tr>
						
						<tr>
							<td>Physical Damage</td>
							<td style="text-align:center;">See "Your Insured Bicycles" Above</td>
							<td style="text-align:center;"> '.$deduc.'</td>
							<td style="text-align:center;">$'.$product_deduc.'.00</td>
						</tr>
						

						<tr>
							<td>Rental Reimbursement</td>
							<td style="text-align:center;">See "$250.00 per occurance</td>
							<td></td>
							<td style="text-align:center;">Included</td>
						</tr>
					
					
						'.$liable_cont.'
						
						'.$medical_cont.'
						
						'.$vehicle_cont.'
						
						<tr>
							<td>Roadside Assistance</td>
							<td style="text-align:center;"></td>
							<td></td>
							<td style="text-align:center;">Included</td>
						</tr>
							
						<tr>
							<td></td>
							<td style="text-align:center;"><b>Total Policy Premium</b></td>
							<td></td>
							<td style="text-align:center;"><b>$ '.$policy_total.'.00</b></td>
						</tr>	
					</table>
					
					<table class="pic">
						<tr>
							<td class="pic-td-full">
								<span class="med-black">FORMS AND ENDORSEMENTS APPLICABLE TO THIS POLICY</span>
							</td>
						</tr>
						<tr>
							<td><br /> SIBY DS 01<br />
							'.$sta.'		
							</td>
						</tr>
					</table>					
				</div>
			<!--  END PERSONAL INFO CONTAINER  -->
				
			</div>
		<!--  END DECLARATION  -->
		
		</div>
	</div>
<br /><br />'; 	
				}
			}  //  END GRAB Open Order ID
		}
	}
//  END PRINT POLICY  //
return $this->policy_html;
	} //  END Print Policy Function
?>

Open in new window

0
Marco GasiFreelancerCommented:
I can't find anything edidently wrong... Have you tried to make the function return $order_items so you can use var_dump to see if it is what you expect it be?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgranlundAuthor Commented:
Through this exercise, I realize that info is being stored in my db in a different table and I think it will be better if I re-write the query first.
0
Marco GasiFreelancerCommented:
Lol, thanks for points. Sometimes we struggle and fight against a fake enemy :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.