【问题标题】:Implementing a basic Twitter/Facebook @mention system [closed]实现一个基本的 Twitter/Facebook @mention 系统 [关闭]
【发布时间】:2013-06-07 03:37:51
【问题描述】:

我正在尝试为我正在创建的微博应用程序创建一个基本的@提及系统(这是我学习 PDO 的项目)。

我读了一些其他的similarquestionsthreads,但我还是听不懂大家在说什么。

我目前拥有的是一个数据库结构,如下所示:

**mentions:** <br>
mention_id (primary key auto increment) <br> 
post_id (id from the posts table) <br> 
user_id (id of the user who is mentioned) <br>
unread (either 0 or 1 depending on whether the post has been viewed by the user mentioned)

**posts:** <br>
post_id (primary key auto increment) <br> 
user_id (id of the user who posted) <br> 
post_content (the post contents)<br> 
stamp (the post time stamp)

我认为它必须根据以前的帖子工作的方式是这样的 - 我们将帖子添加到数据库中,然后在其上运行一个函数来获取 post_id 然后我们对其运行正则表达式语句并提取所有引用到@something,然后我们砍掉@符号并通过一个函数运行它来检查是否有该名称的用户。如果有该名称的用户,我们将他们的 user_id、post_id 和 1 插入到数据库的提及表的未读列中,以便稍后我们可以检查当前登录的用户是否有任何未读的提及并显示这些。我知道这种方法可能无法很好地扩展,但我不打算拥有数百万用户,我只是想要一个简单的解决方案。

所以...我正在寻找的人可以看看我到目前为止所拥有的东西,让我知道如何让它发挥作用和/或提出更好/更简单的方法。

我想要的最终结果是让用户能够@提及另一个用户并能够将其显示为“未读”提及,直到提及的用户查看他们的通知(提及列表等,例如 Facebook) .

到目前为止,我所拥有的是:

$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']->prepare($sql); 
    $stmt->execute(); 
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $rows;
}

function valid_username($mentionedUser){
    $sql = "SELECT 
              username
            FROM users
            WHERE username = $mentionedUser
          "; 
    $stmt = $GLOBALS['db']->prepare($sql); 
    $stmt->execute(); 
    $rows = $stmt->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']->prepare($sql);                                      
    $stmt->bindParam(':user_id', $mentionedUser, PDO::PARAM_STR);       
    $stmt->bindParam(':post_id', $post_id, PDO::PARAM_INT);    
    $stmt->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);   
    }

我什至在正确的轨道上的任何地方?我怎样才能使这项工作?请,您的答案越详细越好,我对您将使用的语法感兴趣,而不仅仅是一般概述。

【问题讨论】:

  • learning PDO开始
  • @YourCommonSense 谢谢,我看了看,也看了一些链接。仍然没有让我很清楚。我的逻辑完全不成立吗?您将如何以一种类似初学者的简单方式来处理它?
  • "所以...我正在寻找的人可以看看我到目前为止所拥有的东西,并让我知道如何让它发挥作用和/或建议一个更好/更简单的方法。”这是非常开放的。为获得最佳结果,请确定您当前方法中的特定问题并修剪您的问题以解决该部分。根据需要重复未来的问题。

标签: php mysql regex database pdo


【解决方案1】:

首先不要选择逐个帖子内容来获取帖子ID!你必须反其道而行之! (从帖子 ID 中选择内容) 你必须做什么:

Get post content from any source (hardcoded, user input)
Insert it in database.
post-id = Get last inserted id
get all mentions
foreach mention,  then 
     get the mentionned user
     insert it in database
Done

我不会为你写,但我建议你单独检查: 插入帖子 提取提及(print_r 会告诉你 $mentions 包含什么,所以用它来调试) 插入提及参考。

我认为您如何使用 $mentions 变量的内容存在问题。

valid_username 也不会返回布尔值,但您使用的是原样 (if(!...)),所以您的逻辑在这里可能不好(至少是函数名称/内容/用法)

你好,对于你使用的每一个函数,检查http://php.net/docs.php相信我,它会帮助你通过了解你使用的逻辑背后的逻辑来编写更多的逻辑代码。

【讨论】:

  • 感谢 Atrekaur,我会退后一步,采用您的方法。 :)
猜你喜欢
  • 2012-11-07
  • 2016-08-21
  • 2011-01-12
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多