3

Hi I have the following php foreach loop,

 <?php        

  foreach ($row['row'] as $inrow) {                
  $slots_available = $inrow[2];              
  $cal_id = $inrow[3];
  $selenable=true;

          ?>

        <tr style="width: 100%;background: <?php if($slots_available=='H') echo "rgb(235,235,235)"; else echo 'white'; ?>;" class="tbody_vo_edit">

          <td class="thc_vo tblb" width="200" style="border-left: 1px dotted #CFCFCF;font:11px verdana;color:#17375e;background: <?php if($slots_available=='H') echo "rgb(235,235,235)"; else echo 'white'; ?>;"><?= $inrow[0] ?></td>

          <td class="thc_vo tblb" width="210" style="border-left: 1px dotted #CFCFCF;font:11px verdana;color:#17375e;background: <?php if($slots_available=='H') echo "rgb(235,235,235)"; else echo 'white'; ?>;"><?php if($slots_available=='H') echo ' '; else { echo $inrow[1]; } ?></td>

          <td class="thc_vo tblb " width="140" style="font:11px verdana;color:#17375e;background: <?php if($slots_available=='H') echo "rgb(235,235,235)"; else echo 'white'; ?>;">

        <div style="float:right;margin-top:-2px;display:<?php if($slots_available=='H') echo "none"; else echo 'block'; ?>">(<?=$users_occupied?>/<?=$slots_available?>)</div>
        </td>

        <td class="thc_vo tblb" style="width: 25%;min-width:141px;max-width:141px;padding:5px;background:<?php if($slots_available=='H') echo "rgb(235, 235, 235)"; else echo 'white'; ?>"><?php if($selenb){?><input style="display:none;" type="text" value="" class="tblinput"><?php }?></td>
        </tr>
        <?php
              }

?>

On execution of this i got a table, in which i would like to compare the values of first ie.,$inrow[0] value with the second row first value. And i would like to add another tag if both the values are different. Can anyone help me how can i compare these values. For example : the table is as follows:


time | tasks | slots

05:00 am - 05:55 am | asdfas | 3

05:00 am - 05:55 am | asdasdfasdf | 4


From this table i want to compare the time values of first row and second row and if both are not equal i would like to add a row in the table between these two rows to say "next time slot" Thanks in advance..

2
  • Maybe you need to use for instead of foreach to get access to previous/next indexes? Commented May 24, 2013 at 6:22
  • no i cant use a for loop here... we can compare these values even if we use the foreach loop but dont know exactly how can we do that Commented May 24, 2013 at 6:35

2 Answers 2

2

You can define a variable to hold the "time" value of the previous loop. In each loop check current "time" value with the previous one and if they are different render an extra row.
Sample code:

$lastTime = "";
forEach (...) {
    $time = $inrow[0];
    ...
    $newSlot = strCaseCmp($time, $lastTime);
    $lastTime = $time;
    if ($newSlot) {?>
        <html_for_"new-slot"_row_goes_here...>
    <?php } ?>

    <html_for_current_timeslot_row_goes_here...>
    ...
<?php } ?>

See, also, this short demo.
(I also took the liberty of slightly modifying your original code.)

Sign up to request clarification or add additional context in comments.

Comments

0

first declare a temp variable and assign a null value to it than first compare the temp variable with $inrow[2] do your stuff and than assign $inrow[2] to temp variable for the next loop execution process

<?php        
 $temp='';  
  foreach ($row['row'] as $inrow) {                  
  $slots_available = $inrow[2];                
  $cal_id = $inrow[3];  
  $selenable=true;  
if($slots_available==$temp){  
 some code according to your need and also change the condition according to needs  
}  
   $temp=$inrow[2];  
          ?>  
        <tr style="width: 100%;background: <?php if($slots_available=='H') echo "rgb(235,235,235)"; else echo 'white'; ?>;" class="tbody_vo_edit">

          <td class="thc_vo tblb" width="200" style="border-left: 1px dotted #CFCFCF;font:11px verdana;color:#17375e;background: <?php if($slots_available=='H') echo "rgb(235,235,235)"; else echo 'white'; ?>;"><?= $inrow[0] ?></td>

          <td class="thc_vo tblb" width="210" style="border-left: 1px dotted #CFCFCF;font:11px verdana;color:#17375e;background: <?php if($slots_available=='H') echo "rgb(235,235,235)"; else echo 'white'; ?>;"><?php if($slots_available=='H') echo ' '; else { echo $inrow[1]; } ?></td>

          <td class="thc_vo tblb " width="140" style="font:11px verdana;color:#17375e;background: <?php if($slots_available=='H') echo "rgb(235,235,235)"; else echo 'white'; ?>;">

        <div style="float:right;margin-top:-2px;display:<?php if($slots_available=='H') echo "none"; else echo 'block'; ?>">(<?=$users_occupied?>/<?=$slots_available?>)</div>
        </td>

        <td class="thc_vo tblb" style="width: 25%;min-width:141px;max-width:141px;padding:5px;background:<?php if($slots_available=='H') echo "rgb(235, 235, 235)"; else echo 'white'; ?>"><?php if($selenb){?><input style="display:none;" type="text" value="" class="tblinput"><?php }?></td>
        </tr>
        <?php
              }  
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.