簡體   English   中英

在 if else 語句中執行 while 循環

[英]do while loop in if else statement

我希望我的代碼做的是在主頁上顯示來自數據庫的所有議程條目,這些條目的日期晚於當前日期。

我選擇我的所有活動條目並應顯示在主頁上。 當我使用 Do While 循環時,這很好用。 所以我想把 DO While 循環放在 If Else 語句中,所以當議程日期晚於 currentDate 時,將顯示 Do While 循環,否則文本 $empty...

顯然我的想法是錯誤的...我不經常使用 PHP,所以有人可以告訴我我做錯了什么或者哪個代碼更適合使用?

提前致謝。

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>';
            }
?>

您必須使用 while 語句來循環獲取的數據。

$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>';
            }
}

這應該讓你的代碼工作。 您需要遍歷獲取的結果以獲取輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM