簡體   English   中英

如何在while循環中對元素進行排序?

[英]How do I sort elements in a while loop?

我將在頁面上顯示評論,一切正常,但我想先顯示用戶的評論。 那么,如何首先顯示來自user_id的用戶評論? 我正在使用while循環來顯示評論。

如果有人可以幫助我,我將不勝感激。 :)

                <?php
                    $sql = "SELECT * FROM `comments` WHERE `post_id`=$pid AND `active`=1 ORDER BY ups-downs DESC";
                    $result = $mysqli->query($sql);
                    $count = $result->num_rows;

                    if($count == 1){
                        $counttext = "1 Comment";
                    }else{
                        $counttext = $count ." Comments";
                    }
                ?>
                <div class="media-area media-area-small">
                    <h3 class="text-center"><?php echo $counttext; ?></h3>
                <?php
                    while($row = $result->fetch_assoc()){
                        $uid = (int)$row["user_id"];
                        $user = get_user_info($mysqli,$uid);
                ?>
                    <div class="media">
                        <a class="pull-left" href="#">
                            <div class="avatar">
                                <img class="media-object" src="<?php echo $user["picture"]; ?>" alt="...">
                            </div>
                        </a>
                        <div class="media-body">
                            <table width="100%">
                                <tr valign="middle">
                                    <td align="left"><h4 class="media-heading text-left"><?php echo fb_clear_name($mysqli, $user["id"]); ?></h4></td>
                                    <td align="right"><h6 class="pull-right text-muted"><?php echo $row["ups"] - $row["downs"]; ?> bits</h6></td>
                                </tr>
                            </table>
                            <p><?php echo htmlentities($row["content"]); ?></p>
                            <div class="media-footer">
                                <a href="#" class="btn btn-info btn-simple "> <i class="fa fa-reply"></i> Reply</a>
                                <a href="#" class="pull-right text-muted">
                                    <?php echo $row["timestamp"]; ?>
                                </a>
                            </div>    
                        </div>
                    </div>
                <?php
                    }
                ?>

問候

您可以預過濾數據。 例如使用

$user_comments=array();
$other_comments=array();

while($row = mysql_fetch_assoc($result))
{
    if($row['user']==$current_user)
   {
        $user_comments[]=$row;
   }
   else
  {
        $other_comments[]=$row;
  }
}
$comments=array_merge($user_comments,$other_comments);

然后,您可以按照自己的方式顯示信息。

暫無
暫無

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

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