Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create mention like twitter or convore with php

hello im just curious. about how they do stuff. what i assume they do something like this

@someone1 im stacking on stackoverflow RT @someone2 : hello guys what are you doing?

before i do it in my way i want to tell you about my database scheme

// CID = COMMENT ID, BID  = BLOG ID, UID = USER ID
CID    BID   UID    COMMENT
1       1     1      @someone1 im stacking on stackoverflow RT @someone2 : ....
2       1     4      @someone1 im stacking on stackoverflow RT @someone2 : ....
3       1     12     @someone1 im stacking on stackoverflow RT @someone2 : ....
  1. they use regex to do like this to take the @someones name

    preg_match_all("/@[a-zA-Z0-9_]+/", $text, $matches);
    
  2. then they get the @ off each name

    foreach ($matches as $value) {
    foreach ($value as $value) {
        $usernames[] = substr($value, 1);
    }
    }
    
  3. then they get the UID from the database from doing something like this

    foreach ($username as $value) {
    # insert database one by one ? so it will be like the example above
    }
    

then we can just output the comment buy geting the UID.

then somhow we can get all the comments in the blog. ( without a same comment ) where blog buid = 1 and give them an notification on every user by where uid = :uid.

is there any better way doing this ? something like twitter or convore ?

Thanks for looking in

Adam Ramadhan

like image 485
Adam Ramadhan Avatar asked Mar 02 '11 14:03

Adam Ramadhan


3 Answers

I have done something similar to this with an in-house application that we use for communication.

Basically, you are going to have two tables: status_updates and mentions. Each status update has many mentions. Whenever someone creates a status update, you save it to the status_updates table. During this process, you can also use Regex to detect any @username "mentions". When you find a mention, you add it to your mentions table. For example, your mentions table might look something like this:

 mention_id (Auto-incrementing key) | status_message_id | username_id

That way if you want to see if someone is mentioned in a status message you can do a quick lookup in the status_messages table, as opposed to loading up the status message and running the Regex each time. The other nice thing about this approach is that it allows you to have multiple mentions in each status message. Just create a record in mentions for each.

That's the basic way that we have set it up.

EDIT: If you wanted to pull an "activity feed" for a given user, showing only the status updates in which they have been mentioned, it would be as simple as:

SELECT * FROM mentions m LEFT JOIN status_messages s ON m.status_message_id = s.id WHERE m.username_id = $username_id

I should note that this is not how they do it at Twitter, because they are dealing with issues of scale that would make this simple way of doing things impossible. However, I think this is the simplest solution that works well if you aren't worried about scaling to hundreds of thousands of users. If you are, then you probably have more issues on your hands than this.

like image 99
Riley Dutton Avatar answered Nov 13 '22 04:11

Riley Dutton


You can use it like bb codes but instead of taken it like [foo] [/foo] you take the @ and end it at the space ... before it's insert into your database you take another script and break the @ after the space. and put the mention into a separate column then use bbcodes to make the mention on the fly

Example..

  if ( strstr("$status", "@") ) {


$explodeat = explode("@", $status);
$explodeat1 = explode(" ", $explodeat[1]);

$status=$explodeat1[0];
}

and insert $status into your mentions column in your database... The BB code for it after that won't be so hard

like image 35
mrstanfan Avatar answered Nov 13 '22 05:11

mrstanfan


I think in MySQL, you can use DISTINCT to avoid duplicates rows:

Something link this:

SELECT `CID`, `BID`, DISTINCT `COMMENT` 
FROM comments
WHERE UID = :uid
AND ##Others clauses for bloc here##
like image 1
Akarun Avatar answered Nov 13 '22 06:11

Akarun