在if else语句中执行while循环

问题描述 投票:0回答:1

我想要我的代码执行的操作是在主页上显示数据库中所有日期早于当前日期的议程条目。

我选择所有我的活动条目,这些条目应显示在主页上。当我使用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>';
            }
?>
php mysql if-statement do-while
1个回答
0
投票

您必须使用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>';
            }
}

这应该可以使您的代码正常工作。您需要遍历获取的结果以获取输出。

© www.soinside.com 2019 - 2024. All rights reserved.