【问题标题】:How to store Profile Views to show each user who's viewed their profile?如何存储个人资料视图以显示查看过他们个人资料的每个用户?
【发布时间】:2012-04-24 15:53:45
【问题描述】:

如果我们想向每个用户显示哪些异性用户查看了他们的个人资料,那么在 MySQL 中跟踪所有这些查看的最佳方式是什么?

每个用户都有一个来自主Users 表的唯一userid,该表还存储了他们的sex

我们希望向每个用户显示查看过他们的用户按从最近视图到最旧视图的顺序

如果用户碰巧查看了自己的个人资料,我们显然不想向他们展示自己。

我们希望只向男生展示看过她们的女孩,而只向女生展示看过她们的男生。

  1. 我们将如何设置ProfileViews 来做到这一点?
  2. 我们会使用哪些索引
  3. 我们需要向查看过它们的每个用户显示什么查询

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    这是我将为您制作的一个简单示例,希望对您有所帮助。

    SQL:

    CREATE TABLE user
    (
        user_id BIGINT NOT NULL AUTO_INCREMENT,
        sex VARCHAR(10) NOT NULL,
        CONSTRAINT PK_USER PRIMARY KEY (user_id)
    ) ENGINE=INNODB;
    
    
    CREATE TABLE profileview
    (
        profileview_id BIGINT NOT NULL AUTO_INCREMENT,
        user_id BIGINT NOT NULL,
        visitor_user_id BIGINT NOT NULL,
        date_time DATETIME NOT NULL,
        CONSTRAINT PK_PROFILEVIEW PRIMARY KEY (profileview_id)
    ) ENGINE=INNODB;
    
    ALTER TABLE profileview
    ADD FOREIGN KEY FK_PROFILEVIEW_USER(user_id) 
    REFERENCES user (user_id);
    
    ALTER TABLE profileview
    ADD FOREIGN KEY FK_PROFILEVIEW_VISITOR(visitor_user_id) 
    REFERENCES user (user_id);
    

    PHP:

    这是用户个人资料页面的一个简单示例 - www.domain.com/profile.php?id=xxx。 此时您需要在用户登录站点时在 session 中定义两个变量: $_SESSION['user_id'] (int) / $_SESSION['user_logged'] (boolean)

    <?php
    if ($_GET && isset($_GET['id']){
    
        if(isset($_SESSION['user_id']){
           $profile_user_id = $_GET['id'];
    
           // the user that visits the profile has a session with his/her id on it.
           session_start();
           $visitor_user_id = $_SESSION['user_id'];
        } else {
           // if visitor specified an id but there is no session, redirect to login.
           header("location: login.php");
        }
    } else {
        // if no id passed redirect to index
        header("location: index.php");
    }
    ?>
    <html>
    <head>
    <title>Your title</title>
    </head>
    <script src="scripts/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
    //here you will store the visit with jquery.
    $(document).ready(function(){
      // store the values from php.
      var profile_user_id = <?php echo $profile_user_id ?>;
      var visitor_user_id = <?php echo $visitor_user_id ?>;
    
      // here, the user information goes to the visit.php file.
      $.post('visit.php' { profile_user_id:profile_user_id, visitor_user_id:visitor_user_id } );
    });
    </script>
    <body>
    Here print user information from a SQL select or something of the id passed in the GET.
    </body>
    </html>
    

    现在,visit.php 文件来存储数据:

    <?php
    if ($_POST && isset($_POST['profile_user_id']) && isset($_POST['visitor_user_id'])) {
    
        session_start();
    
        // this will end the execution of the script if there is no session from user logged
        if ($_SESSION['user_logged'] != true) {
          exit();
        }
    
        // everything is ok, do the process:
        // functions.php contains your SQL connection, I suppose you know how to do it.
        include('../cgi-bin/functions.php');
        $link = dbconn();
    
        $profile_user_id = mysql_real_escape_string($_POST['profile_user_id']);
        $visitor_user_id = mysql_real_escape_string($_POST['visitor_user_id']); 
    
        // this will store the data in profileview including date and time if id's are not equal.
        if($profile_user_id != $visitor_user_id){
           $sql = "INSERT INTO profileview (user_id, visitor_user_id, date_time) VALUES ($profile_user_id, $visitor_user_id, NOW())";
           mysql_query($sql, $link);
        }
    }
    ?>
    

    EXTRA:如果你不知道functions.php做什么,这里是:

    <?php
    
    function dbconn() { 
    if(!include_once('db.php')) { 
      die('Error include file.'); 
    }
    
    if (!$link = mysql_connect($db['hostname'],$db['username'],$db['password'])) { 
      die('Error connecting.'); 
    }
    
    if (!mysql_select_db($db['database'])) { 
      die('Error selecting.'); 
    }
    
    return $link; 
    }
    ?>
    

    上面的文件也需要这个文件:在这里设置你到你的数据库的连接参数。

    db.php

    <?php
      $db = array( 
       'hostname' => 'localhost', 
       'username' => 'root', 
       'password' => 'mysql', 
       'database' => 'mydb' 
      );
    ?>
    
    • 我建议您将其放在主机的 cgi-bin 文件夹中以获得更好的做法,如您在 visit.php 文件代码中所见。

    现在,创建另一个名为visitors.php?id=xxx 的文件,并根据user_id 对您的个人资料视图进行select * from。此时您将能够:

    • 获取user_id 信息,如果是男性(例如)...

      • 按性别选择访问者并制定规则,只列出女性访问者。
      • 根据 profileview 表中存储的时间列出访问者。

    【讨论】:

      【解决方案2】:

      个人资料浏览:

      profile
      userwhoviewed
      timestamp
      

      索引配置文件列。

      因此,当您的用户查看页面时,请检查其是否是个人资料所有者,获取个人资料所有者的性别,检查查看者的性别,如果不同,请使用查看者和时间戳更新表格。

      查询结果时,只需选择与目标配置文件匹配的所有行,按时间戳 desc 排序,然后迭代以将链接构建回这些配置文件。

      我通常在这些字段中使用 INT 数据类型(使行更小并加快查找速度),然后有一个用户表将这些 UID 生成为 auto_increment 主键。这也将保存您的性别和偏好字段,以及任何其他辅助用户数据,如果需要,可以更轻松地更改登录名。

      但是你忽略了你的同性恋用户。最好将它们全部记录下来,让用户根据自己的喜好进行过滤。 :)

      【讨论】:

      • 如果 User1 在不同的时间查看了 User2 两次,我应该记录这两个视图,还是用最新的视图替换旧视图?我该怎么做?
      • 取决于你想要什么,如果你想跟踪总观看次数,那么你会在另一列中添加views,例如,当你更新记录时,拉取当前值并增加它,这应该是微不足道的,因为无论如何您都必须检查表以查看它是新查看器还是回访,以确定您是在执行插入还是更新。
      • 但是我将如何处理这些多视图?例如,如果 User1 在不同时间查看了 User2 两次,我不希望 User1 在 User2 的“谁查看过我”下出现两次。那我该如何处理呢?
      • 我将改写我之前的声明。 1) 您将另一列 views 设置为 INT。 2)当用户查看个人资料时,您检查users 表a)用户不是个人资料所有者b)用户是所需的性别3)如果您想登录该用户,然后检查profileviews 看看这是否用户之前查看过此配置文件,如果否,则插入一个新条目,views 值为 1,如果是,则获取该记录的 views 列的当前计数,增加它,然后更新该行具有新值和当前时间戳的表。
      【解决方案3】:
      UsersTable
      UserID      Sex
      1           Boy
      2           Girl
      3           Girl
      
      
      UsersViewsTable
      UserID      View    Unixtimestamp
      1           2       342143243432
      1           3       142143243432
      2           3       242143243432
      3           1       442143243432
      

      当您访问用户个人资料时,您将使用这个:

      IF CurrentUserSex != UserProfileSex
          INSERT INTO UsersViewsTable (UserID, View, Unixtimestamp) VALUES (CurrentUser, CurrentProfileUserID, time)
      

      现在,您想在页面上获取此内容以查看最后一次看到的异性吗?

      SELECT * FROM UsersViewsTable LEFT JOIN UsersTable USING (UserID) WHERE Sex != CurrentUserSex GROUP BY View ORDER BY Unixtimestamp DESC
      

      编辑:

      IF CurrentUserSex != UserProfileSex {
          $Res = SELECT CurrentProfileUserID FROM UsersViewsTable WHERE UserID = CurrentUserID AND View = UserProfileID LIMIT 1
      
          if($Res['Count'] == 1){
              // Exist
              UPDATE UsersViewsTable SET Unixtimestamp = time WHERE UserID = CurrentUserID AND View = UserProfileID LIMIT 1
          } else {
              // Doesnt exist
              INSERT INTO UsersViewsTable (UserID, View, Unixtimestamp) VALUES (CurrentUser, CurrentProfileUserID, time)
          }
      
      }
      

      【讨论】:

      • 你会如何处理重复的视图?例如,如果 User1 在不同的时间查看了 User2 两次,我应该记录两个视图,还是将旧视图替换为最新视图?我该怎么做?
      • 好了,看看我的编辑...但是,如果我是你,我会做的是:首先你在其他地方创建行的“备份”(外部数据库,另一个表,甚至每个用户的日志文件都没有关系),然后您更新该行。我看到的原因:您可以跟踪内容等...但是从这一点开始,您可以做的是添加一个名为 Count 的字段,每次都执行 +1 和另一个名为 LastVisit 的字段,这样您就可以跟踪 FirstVisit 字段(第一次) 和 lastvisit 在上次访问时更新,因此您可以查看第一次和最后一次访问以及计数
      【解决方案4】:

      只需检查每个用户个人资料页面与访问者 ID 和个人资料 ID 的比较。如果两个不同,则在访问表中存储日期和时间以及您所需的信息。在插入之前,只需检查表格行 如果 prof id, vistor id 已经存在,则更新时间,否则只需插入数据。

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-04
        • 2020-12-14
        • 1970-01-01
        • 2011-12-14
        • 2021-03-09
        • 2019-09-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多