来自mysqli连接查询的按字母顺序排列的列表

时间:2014-07-10 19:54:35

标签: php mysqli

我正在尝试使用mysqli连接查询从朋友表中选择用户ID后打印出按字母顺序排列的用户名列表。

好友表包含

id - userid_one - userid_two - friendship_date - friendship_type

Users表有一个非常标准的布局,我真正需要的唯一字段是username(显示)和userid链接两个表

我想要实现的是以下

A
安迪
安娜


鲍比
布赖恩

我正在使用的代码如下。

$ir['userid'] //the current users id

$getfriends = mysqli_query($con,"SELECT * FROM friends INNER JOIN users ON (friends.userid_one = users.userid OR friends.userid_two = users.userid) WHERE (userid_one = '{$ir['userid']}' OR userid_two = '{$ir['userid']}') ORDER BY username") or die("query error");
        $last="";
        if(mysqli_num_rows($getfriends) == 0)
            {
                print"<li>You have no friends....sorry</li>";
            }
        else
            {

                while($gotf=mysqli_fetch_array($getfriends))
                {
                    if($gotf['userid_one'] == $ir['userid']){$friend = $gotf['userid_two'];}else{$friend = $gotf['userid_one'];}
                    $printed_array = explode(",",$printed);
                    if(!in_array($friend,$printed_array))
                    {
                        $username_of_user = get_username_no_link($friend);
                        $current_letter = $username_of_user[0];
                        if ($last != $current_letter) {
                            print"<li class='Label'>{$current_letter}</li>";
                            $username_of_user = get_username_nd_avatar($friend);
                            print"<li>".$username_of_user."</li>";
                            $last = $current_letter;
                            $printed = $printed.",".$friend;
                        }
                        elseif($last == $current_letter){
                            $username_of_user = get_username_nd_avatar($friend);
                            print"<li>".$username_of_user."</li>";
                            $last = $current_letter;
                            $printed = $printed.",".$friend;
                        }
                    }

                }
            }

我在mysqli查询中使用JOIN的经验不多,我的问题是我没有按照下面的例子显示按字母顺序排列列表。

A
安迪

Ť
托马斯

取值
桑德拉
莎莉

UPDATE-- 用户名显示的顺序现在似乎在重新加载页面后随机改变,这进一步混淆了这个问题!

1 个答案:

答案 0 :(得分:0)

一些人在玩完之后现在回答了问题。结束将用户名放入数组中,然后按值按字母顺序对数组进行排序,以及添加$printed_array数组以在打印后将每个朋友添加到其中以防止它们被打印两次。

$usersnames = array();
                while($gotf=mysqli_fetch_array($getfriends))
                {
                    if($printed != ""){$comma = ",";}
                    if($gotf['userid_one'] == $ir['userid']){$friend = $gotf['userid_two'];}else{$friend = $gotf['userid_one'];}
                    $printed_array = explode(",",$printed);
                    if(!in_array($friend,$printed_array))
                    {
                        $username_of_user = get_username_no_link($friend);
                        $usersnames[] = $username_of_user;
                        $printed = $printed.$comma.$friend;
                    }

                }


                sort($usersnames);
                    foreach($usersnames as $name)
                    {
                        $getuserid = mysqli_query($con,"SELECT userid,avatar FROM users WHERE username = '$name' LIMIT 1");
                        $gotuserid = mysqli_fetch_array($getuserid);
                        //var_dump($gotuserid);
                        $displaypic = "<img style='height:30px;vertical-align:middle;margin:5px;' src='".$gotuserid['avatar']."'>";
                            $current_letter = $name[0];
                            if ($last != $current_letter) 
                            {
                                print"<li class='Label'>{$current_letter}</li>";
                                print"<li><a href='profile/".$name."'>".$displaypic.$name."</a></li>";
                                $last = $current_letter;

                            }
                            elseif($last == $current_letter)
                            {
                                print"<li><a href='profile/".$name."'>".$displaypic.$name."</a></li>";
                                $last = $current_letter;
                            }
                    }