ljfreelancer88 Posted May 28, 2014 Share Posted May 28, 2014 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 circlebut 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; } } Quote Link to comment Share on other sites More sharing options...
mogosselin Posted May 29, 2014 Share Posted May 29, 2014 What is the function viewFriendIfExistOnTbl supposed to do? It just returns true or false? Can you show us more code? Quote Link to comment Share on other sites More sharing options...
ljfreelancer88 Posted May 29, 2014 Author Share Posted May 29, 2014 (edited) 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; ?> Edited May 29, 2014 by ljfreelancer88 Quote Link to comment Share on other sites More sharing options...
trq Posted May 29, 2014 Share Posted May 29, 2014 You viewFriendIfExistOnTbl function does nothing to check if two users are friends. It just checks to see if the current user has any record in the friend table. Quote Link to comment Share on other sites More sharing options...
ljfreelancer88 Posted May 29, 2014 Author Share Posted May 29, 2014 (edited) ----------------------------------- | 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 Edited May 29, 2014 by ljfreelancer88 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 29, 2014 Share Posted May 29, 2014 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”. Quote Link to comment Share on other sites More sharing options...
ljfreelancer88 Posted May 29, 2014 Author Share Posted May 29, 2014 I got a tutorial here http://www.9lessons.info/2014/03/facebook-style-friend-request-system.html this is basically what i need. 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.