Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing a basic Twitter/Facebook @mention system
    primarykey
    data
    text
    <p>I'm trying to create a basic @mention system for a microblogging application that I am creating (it's a project for me to learn PDO).</p> <p>I read a few other <a href="https://stackoverflow.com/a/5182301/2332251">similar</a> <a href="https://stackoverflow.com/a/5456339/2332251">questions</a> and <a href="http://www.codingforums.com/showthread.php?t=266258" rel="nofollow noreferrer">threads</a>,but I still can't understand what everyone is saying.</p> <p>What I have at the moment is a database structure, something like this:</p> <pre><code>**mentions:** &lt;br&gt; mention_id (primary key auto increment) &lt;br&gt; post_id (id from the posts table) &lt;br&gt; user_id (id of the user who is mentioned) &lt;br&gt; unread (either 0 or 1 depending on whether the post has been viewed by the user mentioned) **posts:** &lt;br&gt; post_id (primary key auto increment) &lt;br&gt; user_id (id of the user who posted) &lt;br&gt; post_content (the post contents)&lt;br&gt; stamp (the post time stamp) </code></pre> <p>The way I think it has to work based on previous posts is this - we add the post to the database and then run a function over it to grab the post_id THEN we run a regex statement over it and pull out all the references to @something, then we chop off the @ symbol and run it through a function to check if there is a user by that name. If there is a user by that name we insert their user_id, the post_id and a 1 into the unread column into the mentions table of the database so that later we can check if our currently logged in user has any unread mentions and display those. I understand this method may not scale well, but I'm not looking at holding millions of users I just want a simple solution.</p> <p>So... what I'm looking for is for someone to take a look at what I have so far and let me know how I can make it work and/or suggest a better/more simple approach.</p> <p>The end result I want is for a user to be able to @mention another user and to be able to display it as an 'unread' mention until the mentioned user views their notifications (a list of mentions etc e.g. like Facebook). </p> <p>What I have thus far is this:</p> <pre><code>$post_content = 'Yo @jack, what up? I have a new email, jack@bob.com'; // to be later replaced with $post_content = $_POST['post_content']; // right here would be a function add_post that // inserts the post_content, user_id and time stamp into the database function select_post($post_content){ $sql = "SELECT post_id FROM posts WHERE post_content = $post_content "; $stmt = $GLOBALS['db']-&gt;prepare($sql); $stmt-&gt;execute(); $rows = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); return $rows; } function valid_username($mentionedUser){ $sql = "SELECT username FROM users WHERE username = $mentionedUser "; $stmt = $GLOBALS['db']-&gt;prepare($sql); $stmt-&gt;execute(); $rows = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); return $rows; } function insert_mention($post_id, $mentionedUser){ $sql = "INSERT INTO mentions (post_id, user_id, unread) VALUES (:post_id, :user_id, 1) // 1 means unread "; $stmt = $GLOBALS['db']-&gt;prepare($sql); $stmt-&gt;bindParam(':user_id', $mentionedUser, PDO::PARAM_STR); $stmt-&gt;bindParam(':post_id', $post_id, PDO::PARAM_INT); $stmt-&gt;execute(); } add_post($userid, $post_content); $post_id = select_post($post_content); if (preg_match("/\B@[a-zA-Z0-9]+/i", $post_content)) { preg_match_all("/\B@[a-zA-Z0-9]+/i", $post_content, $mentions); $mentions = array_map(function($str){ return substr($str, 1); }, $mentions[0]); foreach($mentions as $mentionedUser){ if(!valid_username($mentionedUser)){ continue; } insert_mention($post_id, $mentionedUser); } </code></pre> <p>Am I even anywhere on the right track? How can I make this work? Please, the more detail in your answer, the better, I'm interested in what syntax you would use not just a general overview.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload