While loop in php not finishing loop -


i'm working on code , trying pull 4 entries repository , keeps pulling of entries.

<?php      while ( $i < 4) {             foreach ($churchevents->getresults() $churchevent){                 ?>                       <tbody>                         <tr>                             <td> <?php echo $churchevent->getstructuredtext('church-events.event-title')->astext(); ?>                             </td>                             <td class="cuntd" style="vertical-align: middle;">                                 <?php echo $churchevent->getdate('church-events.event-start-date')->asdatetime()->format('l, f j, y'); ?>                             </td>                         </tr>                      </tbody>                   <?php $i++; } } ?> 

you have 2 loops, there no reason have two. inner 1 (foreach) iterates through $churchevents, outer 1 (while) not make less.

remove outer while loop , $i++;, , make inner loop loop on first 4 elements.

for this, can use => syntax in foreach statement value of $i, , add if exit loop:

<tbody>   <?php foreach ($churchevents->getresults() $i => $churchevent){     if ($i >= 4) break; ?>       <tr>         <td> <?= $churchevent->getstructuredtext('church-events.event-title')->astext() ?>         </td>         <td class="cuntd" style="vertical-align: middle;">             <?= $churchevent->getdate('church-events.event-start-date')->asdatetime()->format('l, f j, y') ?>         </td>     </tr> <?php } ?> </tbody> 

note tbody should not generated more once, should stay out of loop.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -