如果我们想向每个用户显示哪些异性用户查看了他们的个人资料,那么在MySQL中跟踪所有这些视图的最佳方法是什么?
每个用户都有一个来自主Users
表的唯一userid
,该表还存储他们的sex
。
我们希望按照最近视图到最旧视图的顺序向每个用户显示查看他们的用户。
我们显然不想向用户展示自己,如果他们碰巧查看了自己的个人资料。
我们只想向男孩展示看过它们的女孩,只向女孩展示看过它们的男孩。
- 我们将如何设置
ProfileViews
表来做到这一点? - 我们将使用哪些索引?
- 我们需要向查看过它们的每个用户显示什么查询?
简单的例子,我将为您制作,希望对您有所帮助。
.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"] (整数)/$_SESSION["user_logged"] (布尔值)
<?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>
现在,访问.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);
}
}
?>
额外:如果你不知道什么功能.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
的文件,并根据user_id
对您的个人资料视图进行select * from
。此时,您将能够:
获取
user_id
信息,如果是男性(例如)...- 按性别选择访客,并制定仅列出女性访客的规则。
- 根据配置文件视图中存储的时间列出访问者。
profileviews:
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)
}
}
只需检查每个用户个人资料页面与访客 ID 和个人资料 ID 的比较。如果两个不同,则在访问表中存储日期和时间以及所需的信息。在插入之前,只需检查表格行如果教授 ID、访客 ID 已经存在,则更新时间,否则只需插入数据即可。
谢谢。