Woah holy shit coding in a blog? WOBAUFHBAHFBHBAWGVAHBFAWFN
So in response to this blog and the request by GMLjosea, Im publishing how I made the user feed for my website :) (right now its not active, but everything is in place to implement)Like I said in the last blog, since the 'player' feed is called pretty much every time a registered person goes to the index on alert games, the feed will be generated, showing the activity from everyone that you've added. Simple enough right?Unfortunately, it gets complicated when you have a certain option: 'Only show people the activity that you have also added as a friend.'Since you don't want to go through that trouble every time you generate the feed, you will need to implement it every time you A) add or remove someone as a friend, or B) change your privacy preference in the user options.Lets see how I am implementing this with PHP and MYSQL:0 = They add you, 1 = add each other, 2 = nobody can see your activityFirst you have the code when adding someone as a friend. If they have option 0 or 2, you simply put my field canfeed in the friends table, to 1 or 0 respectively. Otherwise, you'll have to do this://Update their 'canfeed' row - if they friended you first
$canfeed = mysql_fetch_array(mysql_query("SELECT null FROM friends
WHERE user_id='".$friendid."' AND friend_id='".$myid)or die("Server Error: ".mysql_error()));
if ($canfeed) {
$q = mysql_query("UPDATE friends SET canfeed='1'
WHERE user_id='".$frow['id']."' AND friend_id='".$pun_user['id']);
}
if ($myfeedoption == 1) {
//Update other user that they cant feed your entries
$q = mysql_query("UPDATE friends SET canfeed='0'
WHERE user_id='".$friendid."' AND friend_id='".$myid."'")or die(mysql_error());
}
$result = mysql_query("SELECT friend_id FROM friends
WHERE user_id='".$myid."'")or die('Problem fetch friends');
while ($r = mysql_fetch_array($result)) {
$friends[$r['friend_id']] = $r['friend_id']; //not the best method. pointers sound better
}
if (mysql_num_rows($result) > 0) { //If they have friends, check each if they are also friends
$friendfeed = array();
$result = mysql_query("SELECT canfeed, user_id FROM friends
WHERE friend_id='".$myid."' AND user_id IN (".implode(',',$friends).")")or die('Problem implode');
while ($r = mysql_fetch_array($result)) {
if ($r['canfeed'] == 0) //If 0, set to update to 1.
$friendfeed[] = $r['user_id'];
unset($friends[$r['user_id']]); //friendfeed will update 1; friends[] will update 0.
}
//Now you have the people to set canfeed to 1
if (!empty($friendfeed)) {
$result = mysql_query("UPDATE friends SET canfeed='1'
WHERE (friend_id='".$myid."' AND user_id IN (".implode(',',$friendfeed)."))".
" OR (user_id='".$myid."' AND friend_id IN (".implode(',',$friendfeed)."))")or error('Problem with canfeed 1');
}
} //end if mysql_num_rows() > 0
//The rest will be set to 0
if (!empty($friendfeed)) {
$result = mysql_query("UPDATE friends SET canfeed='0'
WHERE friend_id='".$myid."' AND user_id NOT IN (".implode(',',$friendfeed).")")or error(mysql_error());
}
else {
$result = mysql_query("UPDATE friends SET canfeed='0'
WHERE friend_id='".$myid."'")or error(mysql_error());
}
$q = mysql_query("SELECT friends.friend_id, feed.username, feed.describe, feed.indicator,
feed.posttype, feed.published
FROM friends JOIN feed ON friends.friend_id=feed.userid
WHERE friends.user_id='".$myid."' AND friends.canfeed='1'
ORDER BY feed.published DESC LIMIT 20")or die("Feed: ".mysql_error());
while ($r = mysql_fetch_array($q)) {
//do the display stuff :)
sounds great, but since I barely know shit about PHP and MYSQL, I have no clue what's going on after this text:
Will read this later today, but for now I'll just say this:
Please slap yourself in the face at least once for still using mysql_ functions. :)Oh sir Xemic and his OOP PHP :P
@JID: haha. well thats why its all done for you, you dont even need to know :D
@sir Xemic: Actually i use something like $db->query(), but i replaced it with mysql_query just to make it more general.I updated the code. Before it wouldnt update people if you had noone on the friends list, and fixed the last query in the update script.
@JID: And to clarify if you wanna know, basically i pulled the friends of the person, checked if they were friends with the person back and update if necessary. Then everyone else would get updated to not able to get feed.Fair point. I was just thinking that mysql_ would be more common… Is mysqli still good to use? Also, how much slower is mysql_ functions? My older code on my site has it before i found out about it :<
Anyway Im glad you liked it