简体   繁体   中英

do while loop in if else statement

What I want my code tot do is to show all the agenda entries from the DB on the homepage that have a date that is later then the current date.

I select al my entries that are active and should show on the homepage. That works fine when i use a Do While loop. So i thought put the DO While loop in the If Else statement so when the agendaDate is later than the currentDate the Do While loop will be shown and otherwise the text $empty...

Apparently my thinking is wrong... I don't work that often with PHP so can somebody please tell me what i'm doing wrong or which code is better to use?

Thanks in advance.

MySQL

$query_rsAgenda = "SELECT * FROM agenda WHERE active = 1 AND homepage = 1 ORDER BY agendaDatum ASC";
$rsAgenda = mysqli_query($conCMS, $query_rsAgenda)or die(mysqli_error() );
$row_rsAgenda = mysqli_fetch_assoc( $rsAgenda );

PHP

    $titel = $row_rsAgenda['titel'];
    $textShort = $row_rsAgenda['tekstKort'];
    $idee = $row_rsAgenda['id'];
    $dbDate = $row_rsAgenda['agendaDatum'];
    $agendaDate = date("d-m-Y", strtotime($dbDate));
    $currentDate =  date("Y-m-d");
    $empty = "Op dit moment zijn er geen agendapunten";

    if($dbDate >= $currentDate)
      {  do { 
            echo '<div class="tekstBlokHome">';
            echo '<i class="g-mb-20">' . $agendaDate . '</i>';
            echo '<h3 class="abGroenTekst">' .  $titel . '</h3>';
            echo '<p>' . $textShort .'</p>';
            echo '<p>';
            echo  '<a href="abAgendaDetail.php?id=' . $idee . '">';
            echo  '<span class="fa fa-angle-double-right">&nbsp;</span>Bekijk</a>';
            echo  '<hr>';
            echo  ' </div>'; 
            } while ($row_rsAgenda = mysqli_fetch_assoc($rsAgenda));
       }
     else 
            { echo  '<div class="tekstBlokHome">';
              echo  '<h3 class="abGroenTekst">' .  $empty . '</h3> ';
              echo  '</div>';
            }
?>

You have to use a while statement to loop through the fetched data.

$query_rsAgenda = "SELECT * FROM agenda WHERE active = 1 AND homepage = 1 ORDER BY agendaDatum ASC";
$rsAgenda = mysqli_query($conCMS, $query_rsAgenda)or die(mysqli_error() );
while($row_rsAgenda = mysqli_fetch_assoc( $rsAgenda )){
    $titel = $row_rsAgenda['titel'];
    $textShort = $row_rsAgenda['tekstKort'];
    $idee = $row_rsAgenda['id'];
    $dbDate = $row_rsAgenda['agendaDatum'];
    $agendaDate = date("d-m-Y", strtotime($dbDate));
    $currentDate =  date("Y-m-d");
    $empty = "Op dit moment zijn er geen agendapunten";
    if($dbDate >= $currentDate)
      {  
            echo '<div class="tekstBlokHome">';
            echo '<i class="g-mb-20">' . $agendaDate . '</i>';
            echo '<h3 class="abGroenTekst">' .  $titel . '</h3>';
            echo '<p>' . $textShort .'</p>';
            echo '<p>';
            echo  '<a href="abAgendaDetail.php?id=' . $idee . '">';
            echo  '<span class="fa fa-angle-double-right">&nbsp;</span>Bekijk</a>';
            echo  '<hr>';
            echo  ' </div>'; 

       }
     else 
            { echo  '<div class="tekstBlokHome">';
              echo  '<h3 class="abGroenTekst">' .  $empty . '</h3> ';
              echo  '</div>';
            }
}

This should get your code working. You need to loop throught your result fetched to get your output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM