Jump to content

Looping Through Arrays


nwalker78

Recommended Posts

Hi, im having trouble looping through an array to achieve the desired layout i want to achieve. I can successfully retrieve the data and output it into 1 long table, however i am wanting to output it as 2 tables see code below

 

The php code i have almost works i know where its going wrong but cant for the life of me thingk how to srt it. to save on post size ive removed the cosmetic side of code.

 

any advice is appreciated.

 


<?php for ($i=1; $i<=2; $i++){ 
 $bttot1 = $bttot1 + $rotadetail['pa_hrs0800'];
 $lttot1 = $lttot1 + $rotadetail['pa_hrs1300'];
 $tttot1 = $tttot1 + $rotadetail['pa_hrs1700'];
 $nttot1 = $nttot1 + $rotadetail['pa_hrs2200'];
 $ettot1 = $ettot1 + $rotadetail['pa_rotaETimeDur'];
?>
 <table align="center" border=0 cellspacing=0 cellpadding=0 width=633>
   <tr>
     <td colspan=7 valign=top style="height:4px; "></td>
   </tr>
   <tr align="center">
     <td width="70" rowspan="2">Week <?php echo $i; ?></td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Breakfast</td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Lunch</td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Dinner</td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Bed</td>
     <td width="120" colspan=2 bgcolor="#DBE5F1">Extra Time</td>
   </tr>
     <tr align="center" bgcolor="#DBE5F1">
     <td width="80" colspan=1>08:00-09:00</td>
     <td width="80" colspan=1>13:30-14:30</td>
     <td width="80" colspan=1>17:00-18:00</td>
     <td width="80" colspan=1>22:00-23:00</td>
     <td width="120" colspan=1>Start</td>
     <td width="60">Duration</td>
   </tr>
   <?php foreach($rotadetails as $rotadetail): ?>
     <tr>
       <td colspan=1><?php echo date("l",$rotadetail['pa_rotaDate']); ?></td>
       <td colspan=1><?php echo $rotadetail['pa_hrs0800']; ?></td>
       <td colspan=1><?php echo $rotadetail['pa_hrs1300']; ?></td>
       <td colspan=1><?php echo $rotadetail['pa_hrs1700']; ?></td>
       <td colspan=1 bgcolor="#DBE5F1"><?php echo $rotadetail['pa_hrs2200']; ?></td>
       <td ><?php echo $rotadetail['pa_rotaETime'];  ?></td>
       <td><?php echo $rotadetail['pa_rotaETimeDur']; ?></td>
     </tr>
   <?php endforeach; ?>
   <tr>
     <td colspan=7 valign=top style="height:4px; "></td>
   </tr>
   <tr>
     <td colspan=1 align="right" valign=top>TOTAL </td>
     <td colspan=1><?php echo $bttot1; ?></td>
     <td colspan=1><?php echo $lttot1; ?></td>
     <td colspan=1><?php echo $tttot1; ?></td>
     <td colspan=1 bgcolor="#DBE5F1"><?php echo $nttot1; ?></td>
     <td colspan=2><?php echo $ettot1; ?></td>
   </tr>  
 </table>
<?php } ?>

post-87593-029920600 1371472497_thumb.jpg how i want to look

post-87593-037345900 1371472877_thumb.jpg how it looks

Link to comment
Share on other sites

  • 2 weeks later...

Hi, im having trouble looping through an array to achieve the desired layout i want to achieve. I can successfully retrieve the data and output it into 1 long table, however i am wanting to output it as 2 tables see code below

 

The php code i have almost works i know where its going wrong but cant for the life of me thingk how to srt it. to save on post size ive removed the cosmetic side of code.

 

any advice is appreciated.

 


<?php for ($i=1; $i<=2; $i++){ 
 $bttot1 = $bttot1 + $rotadetail['pa_hrs0800'];
 $lttot1 = $lttot1 + $rotadetail['pa_hrs1300'];
 $tttot1 = $tttot1 + $rotadetail['pa_hrs1700'];
 $nttot1 = $nttot1 + $rotadetail['pa_hrs2200'];
 $ettot1 = $ettot1 + $rotadetail['pa_rotaETimeDur'];
?>
 <table align="center" border=0 cellspacing=0 cellpadding=0 width=633>
   <tr>
     <td colspan=7 valign=top style="height:4px; "></td>
   </tr>
   <tr align="center">
     <td width="70" rowspan="2">Week <?php echo $i; ?></td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Breakfast</td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Lunch</td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Dinner</td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Bed</td>
     <td width="120" colspan=2 bgcolor="#DBE5F1">Extra Time</td>
   </tr>
     <tr align="center" bgcolor="#DBE5F1">
     <td width="80" colspan=1>08:00-09:00</td>
     <td width="80" colspan=1>13:30-14:30</td>
     <td width="80" colspan=1>17:00-18:00</td>
     <td width="80" colspan=1>22:00-23:00</td>
     <td width="120" colspan=1>Start</td>
     <td width="60">Duration</td>
   </tr>
   <?php foreach($rotadetails as $rotadetail): ?>
     <tr>
       <td colspan=1><?php echo date("l",$rotadetail['pa_rotaDate']); ?></td>
       <td colspan=1><?php echo $rotadetail['pa_hrs0800']; ?></td>
       <td colspan=1><?php echo $rotadetail['pa_hrs1300']; ?></td>
       <td colspan=1><?php echo $rotadetail['pa_hrs1700']; ?></td>
       <td colspan=1 bgcolor="#DBE5F1"><?php echo $rotadetail['pa_hrs2200']; ?></td>
       <td ><?php echo $rotadetail['pa_rotaETime'];  ?></td>
       <td><?php echo $rotadetail['pa_rotaETimeDur']; ?></td>
     </tr>
   <?php endforeach; ?>
   <tr>
     <td colspan=7 valign=top style="height:4px; "></td>
   </tr>
   <tr>
     <td colspan=1 align="right" valign=top>TOTAL </td>
     <td colspan=1><?php echo $bttot1; ?></td>
     <td colspan=1><?php echo $lttot1; ?></td>
     <td colspan=1><?php echo $tttot1; ?></td>
     <td colspan=1 bgcolor="#DBE5F1"><?php echo $nttot1; ?></td>
     <td colspan=2><?php echo $ettot1; ?></td>
   </tr>  
 </table>
<?php } ?>

post-87593-029920600 1371472497_thumb.jpg how i want to look

post-87593-037345900 1371472877_thumb.jpg how it looks

 

 

Try to use the foreach loop for the entire table....for example:

 

<?php foreach($rotadetails as $rotadetail): ?>
 <table align="center" border=0 cellspacing=0 cellpadding=0 width=633>
   <tr>
     <td colspan=7 valign=top style="height:4px; "></td>
   </tr>
   <tr align="center">
     <td width="70" rowspan="2">Week <?php echo $i; ?></td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Breakfast</td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Lunch</td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Dinner</td>
     <td width="80" colspan=1 bgcolor="#DBE5F1">Bed</td>
     <td width="120" colspan=2 bgcolor="#DBE5F1">Extra Time</td>
   </tr>
     <tr align="center" bgcolor="#DBE5F1">
     <td width="80" colspan=1>08:00-09:00</td>
     <td width="80" colspan=1>13:30-14:30</td>
     <td width="80" colspan=1>17:00-18:00</td>
     <td width="80" colspan=1>22:00-23:00</td>
     <td width="120" colspan=1>Start</td>
     <td width="60">Duration</td>
   </tr>

     <tr>
       <td colspan=1><?php echo date("l",$rotadetail['pa_rotaDate']); ?></td>
       <td colspan=1><?php echo $rotadetail['pa_hrs0800']; ?></td>
       <td colspan=1><?php echo $rotadetail['pa_hrs1300']; ?></td>
       <td colspan=1><?php echo $rotadetail['pa_hrs1700']; ?></td>
       <td colspan=1 bgcolor="#DBE5F1"><?php echo $rotadetail['pa_hrs2200']; ?></td>
       <td ><?php echo $rotadetail['pa_rotaETime'];  ?></td>
       <td><?php echo $rotadetail['pa_rotaETimeDur']; ?></td>
     </tr>

   <tr>
     <td colspan=7 valign=top style="height:4px; "></td>
   </tr>
   <tr>
     <td colspan=1 align="right" valign=top>TOTAL </td>
     <td colspan=1><?php echo $bttot1; ?></td>
     <td colspan=1><?php echo $lttot1; ?></td>
     <td colspan=1><?php echo $tttot1; ?></td>
     <td colspan=1 bgcolor="#DBE5F1"><?php echo $nttot1; ?></td>
     <td colspan=2><?php echo $ettot1; ?></td>
   </tr>  
 </table>
<?php endforeach; ?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...