繁体   English   中英

如何存储个人资料视图以显示查看过其个人资料的每个用户?

[英]How to store Profile Views to show each user who's viewed their profile?

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

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

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

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

我们只想向男生显示观看过她们的女孩,而仅向男生显示观看过她们的女孩。

  1. 我们如何设置ProfileViews表格来做到这一点?
  2. 我们将使用哪些索引
  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 ['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; 
}
?>

上面的文件也将需要此文件:在此处设置与数据库的连接参数。

db.php

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

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

  • 获取user_id信息以及是否为男性(例如)...

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

个人资料视图:

profile
userwhoviewed
timestamp

索引配置文件列。

因此,当您的用户查看页面时,请检查其是否为配置文件所有者,获取配置文件所有者的性别,检查查看器的性别,如果不同,则使用查看器和时间戳更新表。

查询结果时,只需选择与目标概要文件匹配的所有行(按时间戳记desc排序),然后迭代以建立指向这些概要文件的链接。

我通常在这些字段中使用INT数据类型(保留较小的行并加快查找速度),然后使用一个用户表将这些UID生成为auto_increment主键。 这还将保留您的性别和首选项字段,以及任何其他辅助用户数据,并在需要时更容易更改登录名。

但是,您将同性恋用户排除在外。 最好只记录所有日志,并让用户根据其首选项进行过滤。 :)

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

}

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

谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM