1

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

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

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

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

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

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

4 回答 4

3

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

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

额外:如果你不知道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; 
}
?>

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

数据库.php

<?php
  $db = array( 
   'hostname' => 'localhost', 
   'username' => 'root', 
   'password' => 'mysql', 
   'database' => 'mydb' 
  );
?>
  • 我建议你把它放在cgi-bin你的主机文件夹中以获得更好的实践,正如你在visit.php文件代码中看到的那样。

现在,创建另一个名为visitors.php?id=xxx的文件,并select * from根据user_id. 此时,您将能够:

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

    • 按性别选择访问者,并制定规则以仅列出女性访问者。
    • 根据 profileview 表中存储的时间列出访问者。
于 2012-04-24T16:56:41.303 回答
1
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)
    }

}
于 2012-04-24T16:44:54.563 回答
1

个人资料:

profile
userwhoviewed
timestamp

索引配置文件列。

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

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

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

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

于 2012-04-24T16:09:32.183 回答
0

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

谢谢。

于 2012-04-24T16:40:31.973 回答