Array for friends list; store, retrieve, and display problem
Posted: Tue Nov 10, 2009 3:00 pm
Hi,
I think the answer to my problem is to create an array, but I'm terrible with arrays and any help or guidance is appreciated.
I have a user table (userID, username) and a friend table (uniqueID, userID, username, frienduserID, friendusername, and confirm [y,n]). If John sends a request to Sally to be friends, the userID and username are John's info and the friendID and friendusername are for Sally.
What I'm trying to achieve: regardless of who sends the friend request, once it is confirmed, both are now friends. i was able to create the sql so a user can view their friends list successfully.
however (the problem), i am not able to create a script to properly show the 'add friend' button. this button should only appear if the friend request has never been sent before. what is tough and i think the core of my problem is that i need to check if john and sally's username match within a record to display the 'add friend' button. how can i do this if john can be in the username OR the friendusername column AND sally can be in either column too?
my sql that retrieves the user's friends if they are just looking at their friends list (this works, maybe this will help someone solve my problem of how to correlate this with friends):
one of my many failed attempts to get the desired results to show the add button only if they were not friends (the results of this, it only shows one of the user's friends from the db if you echo $checkfriends and $checkfriends2 when it should be showing more than that... because it isn't echoing the right info, it is also not working for me by determining to show or not to show the add friend button:
I think the answer to my problem is to create an array, but I'm terrible with arrays and any help or guidance is appreciated.
I have a user table (userID, username) and a friend table (uniqueID, userID, username, frienduserID, friendusername, and confirm [y,n]). If John sends a request to Sally to be friends, the userID and username are John's info and the friendID and friendusername are for Sally.
What I'm trying to achieve: regardless of who sends the friend request, once it is confirmed, both are now friends. i was able to create the sql so a user can view their friends list successfully.
however (the problem), i am not able to create a script to properly show the 'add friend' button. this button should only appear if the friend request has never been sent before. what is tough and i think the core of my problem is that i need to check if john and sally's username match within a record to display the 'add friend' button. how can i do this if john can be in the username OR the friendusername column AND sally can be in either column too?
my sql that retrieves the user's friends if they are just looking at their friends list (this works, maybe this will help someone solve my problem of how to correlate this with friends):
Code: Select all
$var_getFriend = "-1";
if (isset($_SESSION['MM_Username'])) {
$var_getFriend = ($_SESSION['MM_Username']);
}
mysql_select_db($database_connUser, $connUser);
$strTemp ="";
$strTemp = $strTemp . "(SELECT 'friend' AS type,";
$strTemp = $strTemp . " friendTable.id, friendTable.user_id, friendTable.username, friendTable.them_user_id, friendTable.friend_username, friendTable.confirm, friendTable.acceptDate";
$strTemp = $strTemp . " FROM friendTable WHERE friendTable.confirm = 'yes' AND friendTable.user_id = (SELECT userTable.user_id FROM userTable WHERE userTable.username";
$strTemp = $strTemp . "=%s)";
$strTemp = $strTemp . ") UNION (";
$strTemp = $strTemp . "SELECT 'theirfriend' AS type,";
$strTemp = $strTemp . " friendTable.id, friendTable.user_id, friendTable.username, friendTable.them_user_id, friendTable.friend_username, friendTable.confirm, friendTable.acceptDate";
$strTemp = $strTemp . " FROM friendTable";
$strTemp = $strTemp . " WHERE friendTable.confirm = 'yes' AND friendTable.them_user_id = (SELECT userTable.user_id FROM userTable WHERE userTable.username =%s)";
$strTemp = $strTemp . ") ORDER BY acceptDate DESC";
$query_getFriend = sprintf($strTemp, GetSQLValueString($var_getFriend, "text"), GetSQLValueString($var_getFriend, "text"));
$getFriend = mysql_query($query_getFriend, $connUser) or die(mysql_error());
$row_getFriend = mysql_fetch_assoc($getFriend);
$totalRows_getFriend = mysql_num_rows($getFriend);
Code: Select all
<?php if ($totalRows_getFriend > 0) { ?>
<?php
while ($row_getFriend = mysql_fetch_assoc($getFriend)) {
if ($row_getFriend['type']=="friend") {
$checkfriends = $row_getFriend['friend_username'];
} else {
$checkfriends2 = $row_getFriend['username'];
}
}
}
?>
<?php echo $checkfriends; echo $checkfriends2;?>