Jump to content

Social Network Relational Database Problem


ljfreelancer88

Recommended Posts

I am creating a simple social network, and i want the post visible only on its circle of friends but the problem is...
let say user_a, user_b, user_c already registered and user_a and user_c connected/friends already and all their posts and comments are visible on their circle
but when user_b write a post oh his wall, it's also visible to user_a and user_c which i dont want to happen. I dont know what was wrong on codes below.
 
CREATE TABLE IF NOT EXISTS `user`(
    `uid` INT(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
    `uname` VARCHAR(25) NOT NULL,
    `pword` CHAR(60) NOT NULL,
    `fullname` VARCHAR(30) NOT NULL,
    INDEX(`uname`)
) Engine = InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci;

CREATE TABLE IF NOT EXISTS `friend`(
    `fid` INT(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
    `friend_id` INT(11) NOT NULL,
    `my_id` INT(11) NOT NULL,
    `stat` ENUM('0','1') NOT NULL,
    INDEX(`friend_id`, `my_id`),
    FOREIGN KEY(`friend_id`) REFERENCES `user`(`uid`)
    ON UPDATE CASCADE
    ON DELETE CASCADE
) Engine = InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci;

public function viewFriendIfExistOnTbl($uid)
    {
        $query = $this->mysqli->query("SELECT `friend_id` FROM `friend` WHERE `my_id` = '$uid' LIMIT 1");
        if ($query->num_rows > 0) {
            return true;
        }
    }
 

Yes, only true or false. It will check if a user he/she is on the friend table. If not, don't show the post/comment otherwise show post/comment..

Take a look at code below. The logic under else statement is too long so i have to chop it. but the concept is still the same.

 

<?php if (!$db->viewFriendIfExistOnTbl($_SESSION['uid'])): ?>
      <p class="okay">You can now post on your wall, however you can't able to view all your posts without
      <a href="add-friend.php" title="Add Friend">adding a friend</a> on your circle. Remember, know your limit. Read <a href="house-rules.htm" title="House Rules">house rules</a>.</p>
<?php else: ?>

   // All my posts/comments including friend's posts/comments goes here.

<?php endif; ?>

-----------------------------------

|     friend_id     |     my_id    |

-----------------------------------

        2                       1

        3                       1

        1                       2

 

Users and their ID

John Doe (1)

Juan dela Cruz (2)

Jane Doe (3)

Stranger (4)

 

Let say I'm already logged in as John Doe w/c has an ID of 1 and my friends are Juan dela Cruz and Jane Doe. I want to check the friend_id column using that function, since Stranger is not my friend, He wont be able to see posts/comments. If my codes wont be applicable, any changes or my codes are welcome. Thanks

There seems to be a lot of confusion regarding the basic logic.

 

Friendship is obviously a relationship between two entities: Person x is a friend of person y. So where does the second person come into play in your function? I only see a single parameter for a single user ID.

 

I would expect something like this:

function isFriendOf($possibleFriendID, $userID)
{

}

And now you simply check if there's any row with $userID as the my_id and $possibleFriendID as the friend_id.

 

However, I think the whole concept needs some clarification. If person A is a friend of person B, does that automatically make B a friend of A as well? Then how do you prevent people from inviting themselves? Do you require confirmation from the other user?

 

It might be a good idea to choose a different term which makes it clear that one direction doesn't necessarily imply the other direction. Something like “has_invited”.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.