从foreach中删除重复项

时间:2017-09-13 11:13:57

标签: php

我有这个foreach脚本,如果没有找到结果,它确实会显示此消息“没有文章”两次,我该如何解决这个问题?我试图将它从foreach中移出,但没有成功。

非常感谢

$query_show_groupmsg = mysqli_prepare($conn, "SELECT group_message_id, group_message_date, group_message_subject, group_message_body FROM group_messages WHERE group_message_refid = ? ORDER BY group_message_date DESC LIMIT 5");   
              mysqli_stmt_bind_param($query_show_groupmsg, 'i',  $data);

        foreach ($id_gruppo as $data) {

              mysqli_stmt_execute($query_show_groupmsg);
              mysqli_stmt_bind_result($query_show_groupmsg, $group_message_id, $group_message_date, $group_message_subject, $group_message_body);
              mysqli_stmt_store_result($query_show_groupmsg);
              $groupmsg = mysqli_stmt_num_rows($query_show_groupmsg);

              if ($groupmsg > 0) {

                while (mysqli_stmt_fetch($query_show_groupmsg)) {

                  $new_mgsgroup_date = date('d/m/Y, \a\l\l\e h:i a', strtotime($group_message_date));

                  ?>
                  <div class="item">
                    <div class="item-head">
                      <div class="item-details">
                        <?php
                        echo ' <a class="item-name primary-link" href="client_blog_app/client_group_post_view.php?id='.$group_message_id.'"> '.ucfirst(strip_tags(mb_strimwidth($group_message_subject, 0, 350, "..."))).'</a>

                        ';

                        ?>
                        <br><span class="item-label font-blue small">Pubblicato il <?php echo "$new_mgsgroup_date"; ?></span>
                      </div>
                    </div>
                    <div class="item-body"> <?php echo strip_tags(mb_strimwidth($group_message_body, 0, 350,"...")); ?> </div>
                  </div>
                  <?php 
                }

              }else{

                echo '

                  <div class="alert alert-info">No articles yet</div>

                ';

             }

          }

3 个答案:

答案 0 :(得分:0)

将IF ELSE语句放在FOREACH之外,如下所示:

$query_show_groupmsg = mysqli_prepare($conn, "
    SELECT group_message_id, group_message_date, group_message_subject, group_message_body FROM group_messages 
    WHERE group_message_refid = ? 
    ORDER BY group_message_date 
    DESC LIMIT 5
");
$article_count = 0;

mysqli_stmt_bind_param($query_show_groupmsg, 'i',  $data);

foreach ($id_gruppo as $data) {

    mysqli_stmt_execute($query_show_groupmsg);
    mysqli_stmt_bind_result($query_show_groupmsg, $group_message_id, $group_message_date, $group_message_subject, $group_message_body);
    mysqli_stmt_store_result($query_show_groupmsg);
    $groupmsg = mysqli_stmt_num_rows($query_show_groupmsg);

    if ($groupmsg > 0) {

        while (mysqli_stmt_fetch($query_show_groupmsg)) {

            $new_mgsgroup_date = date('d/m/Y, \a\l\l\e h:i a', strtotime($group_message_date));
            $article_count++;
             ?>
             <div class="item">
                 <div class="item-head">
                     <div class="item-details">
                     <?php echo ' <a class="item-name primary-link" href="client_blog_app/client_group_post_view.php?id='.$group_message_id.'"> '.ucfirst(strip_tags(mb_strimwidth($group_message_subject, 0, 350, "..."))).'</a>'; ?>
                     <br>
                     <span class="item-label font-blue small">Pubblicato il <?php echo "$new_mgsgroup_date"; ?></span>
                 </div>
             </div>
             <div class="item-body"> <?php echo strip_tags(mb_strimwidth($group_message_body, 0, 350,"...")); ?> </div>
             </div>
             <?php 
         }
     }
}

if ($article_count == 0){
    echo '<div class="alert alert-info">No articles yet</div>';
}

答案 1 :(得分:0)

由于if ($groupmsg > 0)条件位于foreach周期内,else分支最多可执行count($data)次。将条件移出foreach循环应该可以解决这个问题。您可以在foreach内查看文章的数量,并在foreach检查之后是否存在。

然而,这段代码读起来真的很糟糕(没有评论,随机缩进)。如果需要,您应该制作一个最小的工作示例,删除无用的部分并注释您的代码。

答案 2 :(得分:0)

在for循环之外添加它,并删除已有的else子句。

 <?
 if ($groupmsg < 1)
 {
     ?>
         <div class="alert alert-info">No articles yet</div>
     <?
 }