2012-04-24 46 views
1

如果我们想向每个用户展示哪些异性用户已经查看过他们的个人资料,那么跟踪MySQL中所有这些视图的最佳方式是什么?如何存储配置文件视图以显示每个查看其配置文件的用户?

每个用户都有一个唯一的userid,主Users表中也存储着它们的sex

我们希望向每个用户显示按照最近查看到最早查看的顺序查看它们的用户

我们显然不想让用户看到他们自己的个人资料。

我们只想向男士展示查看他们的女孩,而女孩只查看他们的女孩。

  1. 如何将我们设置ProfileViews这样做呢?
  2. 什么索引我们会用?
  3. 什么是查询我们需要显示每个查看过它们的用户?

回答

1

profileviews:

profile 
userwhoviewed 
timestamp 

指数曲线列。

所以当你的用户浏览页面时,检查它是否是个人档案所有者,获取档案所有者的性别,检查观众的性别,如果不同,用查看器和时间戳更新表格。

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

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

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

+0

如果用户1在不同时间观察用户2两次,我应该记录这两种观点,或更换了最新的观点老观点?我会怎么做? – ProgrammerGirl 2012-04-24 16:18:38

+0

取决于你想要的,如果你想跟踪视图的总数,那么你会在另一列添加'views'例如,然后当你更新记录时,拉取当前值并增加它,这应该是无关紧要,因为无论如何你都必须检查表格,看看它是新的查看器还是回访,以确定你是在插入还是在更新。 – TaoJoannes 2012-04-24 16:33:21

+0

但是,我会如何处理这些多个视图?例如,如果User1在不同时间查看了两次User2,我不希望User1在User2的“谁看我”下显示两次。那么我将如何处理? – ProgrammerGirl 2012-04-24 16:53:45

0

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

谢谢。

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

} 
+0

你将如何处理重复的视图?例如,\t 如果User1在不同的时间两次查看User2,我应该记录这两个视图还是用最新的视图替换旧视图?我会怎么做? – ProgrammerGirl 2012-04-24 16:52:11

+0

还有,看看我的编辑...但是,如果我是你,我应该做的是:首先你在别的地方创建行的“备份”(外部DB,另一个表,甚至每个用户的日志文件,没关系),然后你更新该行。我看到的原因是:你可以追踪东西等等......但是从这一点来说,你可以做的是添加一个名为Let's say Count的字段,并且每次执行+1和另一个名为LastVisit的字段,因此您会跟踪FirstVisit字段(第一次)和上次访问的最后一次访问更新,以便您可以查看第一次和最后一次访问以及计数 – 2012-04-24 17:02:58

2

这是一个简单的例子,我会让你,希望这有助于。

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'(布尔)

<?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。

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信息,如果它是男性(例如)...

    • 按性别选择游客和做一个规则列表中唯一的女性游客。
    • 根据存储在配置文件查看表中的时间列出访问者。
相关问题