2014-12-06 44 views
0

我即将在页面上显示评论,一切正常,但我想先显示用户的评论。那么,如何首先显示来自user_id的用户评论?我使用while循环来显示评论。如何在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 
        } 
       ?> 

问候

+0

给出一些示例代码,以便我们可以更好地帮助您。 – melanholly 2014-12-06 10:21:49

+0

哦,好的,我很抱歉。添加了代码。 – user3434770 2014-12-06 10:30:55

+0

如果您希望以特定方式对数据进行排序,我会在数据库上执行此操作,而不是在您的应用程序代码中进行。有什么阻止你这样做吗? – 2014-12-06 19:55:20

回答

0

您可以预先过滤的数据。例如,使用

$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); 

然后您可以按照自己想要的方式显示信息。

+0

非常感谢!我会稍后再尝试:) – user3434770 2014-12-06 10:32:19

相关问题