php - 我只得到一个奇怪的行而不是2个普通行

标签 php mysql arrays select

问题是我得到一个奇怪的公告,而不是2个正常的公告(如在我的数据库中)。

现在的样子(截图):

https://prnt.sc/q5q8ju

码:

<?PHP $query = "SELECT * FROM `thbs_announcements` ORDER BY `anno_date` DESC";
                $result1 = mysqli_query($db, $query);
                $anno = mysqli_fetch_array($result1, MYSQLI_ASSOC);
                if(mysqli_num_rows($result1) > 0)
                {
                    while($anno = $result1->fetch_assoc());
                    {
                        if($anno['anno_important'] == 1)
                        {
                            ?><div class="card"><h2 style="margin-bottom: 5px;"><?php echo $anno['anno_text'];?> <span class="badge badge-danger">IMPORTANT</span></h2></div><?php
                        }
                        if($anno['anno_important'] != 1)
                        {
                            $today = date("Y-m-d");
                            $dateDiffa = dateDiffInDays($today, $anno['anno_date']);
                            if($dateDiffa < 7 || $dateDiffa > -7)
                            {
                                ?><div class="card"><h2 style="margin-bottom: 5px;"><?php echo $anno['anno_text'];?> <span class="badge badge-primary">NEW</span></h2></div><?php
                            }
                            else
                            {
                                ?><div class="card"><h2 style="margin-bottom: 5px;"><?php echo $anno['anno_text'];?></h2></div><?php
                            }
                        }
                    }
                }
                else
                {
                    ?><div class="card"><h2 style="margin-bottom: 5px;">No announcements here.</h2></div><?php
                }
                ?>

最佳答案

我希望这会起作用

<?PHP $query = "SELECT * FROM `thbs_announcements` ORDER BY `anno_date` DESC";
                $result1 = mysqli_query($db, $query);
                if(mysqli_num_rows($result1) > 0)
                {
                    while($anno = mysqli_fetch_assoc($result1)) 
                    {
                        if($anno['anno_important'] == 1)
                        {
                            ?><div class="card"><h2 style="margin-bottom: 5px;"><?php echo $anno['anno_text'];?> <span class="badge badge-danger">IMPORTANT</span></h2></div><?php
                        }
                        if($anno['anno_important'] != 1)
                        {
                            $today = date("Y-m-d");
                            $dateDiffa = dateDiffInDays($today, $anno['anno_date']);
                            if($dateDiffa < 7 || $dateDiffa > -7)
                            {
                                ?><div class="card"><h2 style="margin-bottom: 5px;"><?php echo $anno['anno_text'];?> <span class="badge badge-primary">NEW</span></h2></div><?php
                            }
                            else
                            {
                                ?><div class="card"><h2 style="margin-bottom: 5px;"><?php echo $anno['anno_text'];?></h2></div><?php
                            }
                        }
                    }
                }
                else
                {
                    ?><div class="card"><h2 style="margin-bottom: 5px;">No announcements here.</h2></div><?php
                }
                ?>

关于php - 我只得到一个奇怪的行而不是2个普通行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58913459/

相关文章:

php - 从 mysql-php(android) 中检索图像

php - 安全地向 Flash 游戏的获胜者提供唯一的密码?

php - 从数据库收到不必要的价格延长

java - 如何使用 Java 数组——从 Ruby 到 Java 的转换

c# - 对象数组与对象属性数组-哪个更有效?

php - 新闻聚合网站托管

php - 创建简单的 Codeigniter 库

MySQL,两个表显示来自两个表的信息

php - 合并两个MySQL表,但希望一列等于另一列

arrays - 从多个数组中提取信息