TaylorSandbek Posted May 8, 2008 Share Posted May 8, 2008 Heres the quick lowdown - making a site where the user will have friends. How to save these friends in a database? surely a table titled friends that stores the friend info will get way to full way too fast, even if I use longtext right? help is appreciated as usual - Taylor (thanks in advance) Quote Link to comment Share on other sites More sharing options...
947740 Posted May 8, 2008 Share Posted May 8, 2008 Even if it is a long way, it still works. Unless you can think of any other ways, that's how I would do it. Quote Link to comment Share on other sites More sharing options...
TaylorSandbek Posted May 8, 2008 Author Share Posted May 8, 2008 Really? Seems like it wouldnt work that way to me, but thanks Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 8, 2008 Share Posted May 8, 2008 I do the same type of thing on my site, and I will be a nice guy and show you how I do it (you don't have to do it this way) fist, I make a table: id = unique id of each friend pair requester = the person who requested to be friends receiver = the person who will receive the request approved = whether the two are actually friends or not (1 = friends, 0 = friend request pending) date = time the friend request was made and/or time the two became official friends next, I use 2 queries, one for getting all your friends, and one for checking individual friends <?php // This checks to see if you are viewing a profile or not if(isset($_GET['id'])){ $uId = $_GET['id']; $prof = FALSE; }else{ $uId = $_SESSION['id']; $prof = TRUE; } $sql = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM friends,users WHERE (requester = '$uId' OR receiver = '$uId') AND( (friends.receiver = users.id AND friends.receiver != '$uId') OR (friends.requester = users.id AND friends.requester != '$uId') )AND users.id != '$uId' AND approved = '1' ORDER BY users.id DESC LIMIT 15"); $result_count = mysql_query("SELECT FOUND_ROWS()")or die(mysql_error()); $total = mysql_fetch_array($result_count); $totalrows = $total[0]; if(mysql_num_rows($sql)<1){ echo '<p>No Friends.</p>'; }else{ while($row = mysql_fetch_array($sql)){ echo $row['username'].'<br />'; // Displays the friends username from the users table } } ?> Make sure you make your query more secure than mine above users is a table that has each individual user in it, such as: username, password, email, etc. Remove LIMIT 15 to select all your friends. The other query is to check a specific friend, to see if he/she is a friend: <?php // This checks to see if you are viewing a profile or not if(isset($_GET['id'])){ $uId = $_GET['id']; $prof = FALSE; }else{ $uId = $_SESSION['id']; $prof = TRUE; } if($_SESSION['id'] != $_GET['id']){ // Make sure this is not your profile and someone elses $sql1 = mysql_query("SELECT * FROM friends WHERE (requester = '$uId' OR receiver = '$uId') AND (requester = '{$_SESSION['id']}' OR receiver = '{$_SESSION['id']}')")or die(mysql_error()); } if(!$prof){ $query = sprintf("SELECT * FROM users WHERE id = '%s'",mysql_real_escape_string($uId)); $sql = mysql_query($query)or die(mysql_error()); $row = mysql_fetch_array($sql); if($_SESSION['id'] != $_GET['id']){ if(mysql_num_rows($sql1)==1){ $row1 = mysql_fetch_array($sql1); if($row1['approved'] == 1) echo '<strong>Already Friends</strong><br />'; else echo '<strong>Friend Pending</strong><br />'; $friend = TRUE; }else{ echo '<span id="friendRequestStatus"><a href="fReq.php?id='.$_GET['id'].'">Become Friends</a></span><br />'; $friend = FALSE; } } } ?> That is basically the code, I did add on piece of code in there 2 times: <?php // This checks to see if you are viewing a profile or not if(isset($_GET['id'])){ $uId = $_GET['id']; $prof = FALSE; }else{ $uId = $_SESSION['id']; $prof = TRUE; } it only needs to be in there one time if these two pieces of code are on the same page (like my site) If you have any questions, feel free to ask. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 8, 2008 Share Posted May 8, 2008 surely a table titled friends that stores the friend info will get way to full way too fast, even if I use longtext right? And why would you use longtext? Were you planning to have a single record for each user with ALL of their firends listed in a specific field? Follow The Little Guy's example. You should have a single record for each friend relationship. Simple smallint fields should suffice. Quote Link to comment Share on other sites More sharing options...
TaylorSandbek Posted May 8, 2008 Author Share Posted May 8, 2008 Thanks T L G! Very helpful.. well actually you kind of did it for me. I wish I understood more of the code itself so I would actually learn. Im just starting out and anything thats not basic ish is gibberish to me still. obviously the kind of site im working on is not ideal for someone just starting lol Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.