2014-07-10 46 views
0

我想在使用mysqli加入查詢從朋友表中選擇userid後,打印出按字母順序排序的用戶名列表。從mysqli加入查詢中按字母順序排列的列表

友表由

id - userid_one - userid_two - friendship_date - friendship_type 

用戶表中有一個非常標準的佈局,唯一的領域我真的從它需要的是username(顯示)和userid鏈接兩個表

我想達到什麼是低於

一個
安迪
安娜


鮑比
布賴恩

我使用的代碼如下。

$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的很多經驗,我的問題是,我沒有得到按字母順序排列爲下面的示例顯示列表。


安迪

Ť
托馬斯

小號
桑德拉
薩利

UPDATE-- 用戶名顯示的順序現在似乎在重新加載頁面後隨機更改,這更加令人困惑!

+0

您有具體問題嗎? –

+0

你有什麼錯誤? – DanDotNet

+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; 
          } 
        } 
相關問題