简体   繁体   English

回声排序多维数组

[英]Echoing Sorted Multidimensional Array

Ok, so I am creating a web app with php and mysqli. 好的,所以我正在用php和mysqli创建一个Web应用程序。 I have a table friends which is a simple set up: 我有一个表friends ,这是一个简单的设置:

f_id int(11)
uid int(11)
fids TEXT

now its basically like a row for each user with the fids consisting of a lot of numerical values (other userids) separated by commas like: 1,2,3 so I use this function to get each user's friends: 现在,对于每个用户而言,它基本上就像一行,其fids由许多数值(其他用户ID)组成,这些数值之间用逗号分隔: 1,2,3因此我使用此功能来获取每个用户的朋友:

function getFriends($db, $userid)
{
    $q = $db->query("SELECT fids FROM friends WHERE uid='$userid'");
    $ar = $q->fetch_assoc();
    $friends = $ar['fids'];
    $fr = explode(",", $friends);

    return $fr;
}

but each posts comments that appear to each of their friends. 但每个帖子都会向他们的每个朋友显示评论。 my problem comes from trying to sort these comments by the time they were posted. 我的问题来自尝试按评论的发布时间对其进行排序。 lets say my comments table is: 可以说我的评论表是:

c_id int(11)
uid int(11)
c_text TEXT
c_time int(11)

I want to be able to get the comments posted by each 'friend' put them all into an array together, then sort them from their c_time value, then all the values from that particular row in the comments table. 我希望能够得到每个“朋友”发布的评论,并将它们全部放入一个数组中,然后从其c_time值对它们进行排序,然后从comments表中该特定行的所有值进行排序。

The problem comes from my how I've set up my friends table. 问题出在我如何设置朋友表。 I'm using: 我正在使用:

$fr = getFriends($db, $userid);
        $updates = array();
        $i = 0;
        foreach( $fr as $friend)
        {
            // Get Updates from friends and from self
            $q = $db->query("SELECT up.*, u.* FROM updates up
                LEFT JOIN users u ON u.id = '$friend'
                WHERE (up.userid = '$userid') ORDER BY up.up_id DESC");
            while($ar = $q->fetch_array(MYSQLI_BOTH))
            {
                $updates[$i] = $ar;
                $i++;
            }
        }
            $sortArray = array(); 

            foreach($updates as $update){ 
                foreach($update as $key=>$value){ 
                    if(!isset($sortArray[$key])){ 
                        $sortArray[$key] = array(); 
                    } 
                    $sortArray[$key][] = $value; 
                } 
            } 
            $orderby = "up_id";
            array_multisort($sortArray[$orderby],SORT_DESC,$updates); 
            $updates_limit = array_slice($updates, 0, 20);

to get the comments from each friend, sorting it by time, then slicing it to the first 20. However when I var_dump($updates_limit) it takes the last row in the comments table, and then makes it look like each friend posted the same comment. 从每个朋友那里获取评论,按时间排序,然后将其切成前20个。但是当我var_dump($updates_limit)它进入评论表中的最后一行,然后使其看起来像每个朋友都张贴相同评论。

Can anyone see the problem or a better way of addressing this issue? 谁能看到这个问题或解决这个问题的更好方法?

I'd completely refactor the friends table to look something more like this: (Also, use english - Characters are cheap :c)) 我会完全重构friends表,使其看起来像这样:(另外,使用英语-字符便宜:c))

CREATE TABLE friends (
    user_id int FOREIGN KEY REFERENCES user(id)
    , friend_id int FOREIGN KEY REFERENCES user(id)
    , PRIMARY KEY (user_id, friend_id)
);

Then you can take essentially the same comment table: 然后,您可以使用基本相同的注释表:

CREATE TABLE comment (
    comment_id int PRIMARY KEY
    , user_id int FOREIGN KEY REFERENCES user(id)
    , comment_text text
    , comment_time datetime
);

And your "query for friend's comments" becomes: 您的“查询朋友的评论”将变为:

SELECT comment_id, comment.user_id, comment_text, comment_time
  FROM friends
    INNER JOIN comment
      ON comment.user_id = friends.friend_id
  WHERE friends.user_id = ? #Target it
  ORDER BY comment_time DESC
  LIMIT 0, 20;

You can even speed this up by adding a few indexes - like comment(user_id). 您甚至可以通过添加一些索引来加快此过程-例如comment(user_id)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM