这是一个简单的例子,我会让你,希望这有助于。
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
。在这一点上,你将能够:
如果用户1在不同时间观察用户2两次,我应该记录这两种观点,或更换了最新的观点老观点?我会怎么做? – ProgrammerGirl 2012-04-24 16:18:38
取决于你想要的,如果你想跟踪视图的总数,那么你会在另一列添加'views'例如,然后当你更新记录时,拉取当前值并增加它,这应该是无关紧要,因为无论如何你都必须检查表格,看看它是新的查看器还是回访,以确定你是在插入还是在更新。 – TaoJoannes 2012-04-24 16:33:21
但是,我会如何处理这些多个视图?例如,如果User1在不同时间查看了两次User2,我不希望User1在User2的“谁看我”下显示两次。那么我将如何处理? – ProgrammerGirl 2012-04-24 16:53:45