Jump to content

HELP with how to go about coding this function


Recommended Posts

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)

Link to comment
Share on other sites

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:

 

121025557922383.jpg

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.