User Feed (mysql and php)

Posted by Alert Games on Jan. 5, 2012, 5:34 p.m.

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 activity

First 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']);
}

And now for the removing of a friend. The only thing that matters is option 1, because the friend row that contains the canfeed indicator is deleted anyway.

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());
}

So that wasn't too bad. Basically you check their option and make the decision right there. The next part is a little irritating, but shouldn't be done often. So there shouldn't be much problem for how many steps it will take.

Now if someone changes their preference, all the canfeed fields need to be updated if needed. This may not be the most efficient way to handle this, but it does get a bit complicated.

Again, if the person chooses option 0 or 2, everyone that friended you should get updated. (that is, if the option is different than before. so its not pointless.) But if you choose option 1, here comes the heavy duty code:

$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());
        }
  

Finally the feed will just pull from the friends table and the feed, only from where canfeed is 1.

$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 :)

So there it is in all its glory (stripped down of course so its not massive and not the same variable names). Now all that needs to be done is to add feed entries with the appropriate data from the game scripts ^_^ Hopefully I was kinda clear in what I was explaining :P

Comments

JID 12 years, 10 months ago

sounds great, but since I barely know shit about PHP and MYSQL, I have no clue what's going on after this text:

Quote:
Again, if the person chooses option 0 or 2, everyone that friended you should get updated. (that is, if the option is different than before. so its not pointless.) But if you choose option 1, here comes the heavy duty code:
:p

sirxemic 12 years, 10 months ago

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. :)

Gift of Death 12 years, 10 months ago

Oh sir Xemic and his OOP PHP :P

Alert Games 12 years, 10 months ago

@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.

Alert Games 12 years, 10 months ago

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.

sirxemic 12 years, 10 months ago

Quote:
Oh shut up. :)
You too slap yourself in the face at least once, then.

Quote:
Actually i use something like $db->query(), but i replaced it with mysql_query just to make it more general.
I suspected so, but I also had an answer prepared for that response: if you wanted it to be more general, you should have used pseudocode. This way you are demotivating people to use something else than mysql_ functions. mysql_ is considered unprofessional and not the fastest nowadays and should simply be avoided.

But besides that minor issue, this blog was a fun read.

Alert Games 12 years, 10 months ago

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