2013-04-18 20 views
4

OK,所以我有headech從到處搜尋,如何解決這個problem..Im試圖顯示在線用戶,但並不是所有的用戶,只有誰是在你的好友列表..Mysql的在線好友顯示

所以我有一個名爲users_online的表,當用戶登錄在我的網站在該表中自動創建1行與日期,IP,名稱,user_id和friend_array(其中所有用戶的朋友保留)

因此,例如我登錄我的網站和行是在users_online表中創建的。我想在網上看到的只是我的朋友,這些朋友都存儲在friend_array列(1,5,16,5(那朋友ID號))。我如何可以從friends_array科拉姆數據,看看這些ID的一個的記錄目前,這意味着這些id中哪些存在於user_online表中並顯示在我的配置文件中?

我希望不是混亂的問題...

那麼這就是我的code..all專賣店在online.php文件:

// Checking wheter the visitor is already marked as being online: 
$inDB = mysql_query("SELECT user_id FROM users_online WHERE user_id=".$userid); 

if(!mysql_num_rows($inDB)) 
{ 
    // Selects some data required to insert into users_online table from users table 
    $DB = mysql_query("SELECT img,fname,friend_array FROM users WHERE user_id=".$userid); 
    while($row=mysql_fetch_assoc($DB)) 
    { 
    $img = $row['img']; 
    $fname = $row['fname']; 
    $farray = $row['friend_array']; 
    } 
    mysql_query(" INSERT INTO users_online (user_id,ip,img,fname,friend_array) 
        VALUES(".$userid.",'".$intIp."','".$img."','".$fname."','".$farray."')"); 
} 
else 
{ 
    // If the visitor is already online, just update the dt value of the row: 
    mysql_query("UPDATE users_online SET dt=NOW() WHERE user_id=".$userid); 
} 


// Counting all the online visitors: 
// Thats where i need to work out with friend array.. 
// I need to display all online friends only 
list($totalOnline) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM users_online")); 


// Outputting the number as plain text: 
echo $totalOnline; 
+0

考慮採取的朋友列到另一個表並存儲每個朋友一個新行,不在陣列中。也不要在每次登錄時插入新行,但更新當前時間戳值。你也可以使用'INSERT ....對重複密鑰更新在線= NOW()' –

+0

嗯,我想到了這一點。要創建另一個表,當他們去離線,你不會得到你希望他們離開後的結果只使用ID,USER_ID和friends_id .. – AlwaysConfused

回答

2
<?php 
    $friends = array(1,5,16); // Array of friends 
    $friendIDs = implode(',', $friends); // Turns array into string for SQL select statement 

    // Gets only friends info from DB 
    $sql = " 
     SELECT date, ip, name, user_id 
     FROM users_online 
     WHERE user_id IN (".$friendIDs.")"; 
?> 
+0

雖然沒有從user_online表中刪除的條目... –

0

你應該考慮改變數據庫模式。這裏有一個例子:

USER表:

id | name | current_session_id 

USER_FRIEND_USER表(包含朋友說,用戶已添加):

user_id | friend_user_id 

會話表:

id | user_id | created_at | expires_at 

SQL查詢來獲取與ID用戶的好友列表「?」:

SELECT u.id, u.name FROM USER u 
    INNER JOIN USER_FRIEND_USER ufu ON (ufu.friend_user_id = u.id) 
    INNER JOIN SESSION s ON (s.id = u.current_session_id) 
    WHERE ufu.user_id = ? AND s.expires_at >= NOW(); 

這裏的SQL小提琴鏈接:http://sqlfiddle.com/#!2/34f83/1/0

2

整理出來的傢伙!謝謝你的幫助!

//Selecting an array from db 
$DB = mysql_query("SELECT friend_array FROM users_online WHERE user_id=".$userid); 
    while($row=mysql_fetch_assoc($DB)) 
    { 
    $friend_array = $row['friend_array']; 
    $friends = array($friend_array); // Array of friends 
    $friendIDs = implode(',', $friends); 
    } 
// Counting all the online friends: 

list($totalOnline) = mysql_fetch_array(mysql_query(" 
     SELECT COUNT(*) 
     FROM users_online 
     WHERE user_id IN (".$friendIDs.")")); 


// Outputting the number as plain text: 
echo $totalOnline; 

輸出爲1個用戶在線,因爲我用2級的瀏覽器和在users_online表被創建2行編號爲1和2..And ID loged在1具有在陣列3周的朋友(2,5,16) ,並且id爲2的用戶擁有(1,3)。因此,在每個瀏覽器中,輸出爲1..Uray!我希望這個問題在您使用更新功能上mSQL的表someone..Btw林,如果用戶註銷,我只是刪除行users_online表:)