我要做的是在我的脚本中添加一个用户跟随系统(类似于Twitter)。
这就是db结构的样子:
CREATE TABLE IF NOT EXISTS `user_follow` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`follower` int(11) NOT NULL,
`following` int(11) NOT NULL,
`subscribed` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `follow_unique` (`follower`,`following`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
如果我想创建我应该使用此代码:
mysql_query(" INSERT INTO `user_follow` (`follower`, `following`, `subscribed`) VALUES ('$follower', '$following', CURRENT_TIMESTAMP); ");
对于取消订阅,它应该如下所示:
mysql_query(" DELETE FROM `user_follow` WHERE `follower` = '$follower' AND `following` = '$following'; ");
$follower = $_SESSION[user_id]; // the user_id for the one who is currently logged id
$following = $user_id; // the user_id for the user profile where the script will be on
在个人资料页面上,我有一个这样的表格:
<?php if (isset($_SESSION[user_id]) && $_SESSION[user_id] != $user_id) { ?>
<form action="" method="post">
<input name="action" type="hidden" value="<?php echo $subscribe_status; ?>"/>
<button name="subject" type="submit" value="<?php echo $subscribe_text.' '.$username; ?>"><?php echo $subscribe_text.' '.$username; ?></button>
</form>
<?php } ?>
我不知道的是如何链接所有这些代码...
编辑:
$subscribe_status
应更改为follow
或unfollow
,具体取决于用户是否已关注该用户(通过查看我认为的查询)。
同样$subscribe_text
应该是Follow
或Unfollow
,具体取决于当前登录的用户($follower
)是否已经关注该用户。
有人可以帮帮我吗?
EDIT 2(基于Mihir Singh的回答)
$user_follow = dbquery(" SELECT * FROM `user_follow` WHERE `follower` = '$follower' AND `following` = '$following'; ");
$check_status = dbrows($user_follow);
$sub = false; //Boolean var which states if subscribed or not
if ( $check_status !== 0 ){ //Pseudo code
$sub = true; //If row is found, they are subscribed, so set $sub to true
}
if($sub){
$subscribe_status = "follow";
$subscribe_text = "Follow";
}
else{
$subscribe_status = "unfollow";
$subscribe_text = "Unfollow";
}
答案 0 :(得分:2)
以下是一些代码:
$sub = false; //Boolean var which states if subscribed or not
if (row in table exists --> subscribed ){ //Pseudo code
$sub = true; //If row is found, they are subscribed, so set $sub to true
if($sub){
$subscribe_status = "Follow";
$subscribe_text = "Unfollow";
}
else{
$subscribe_status = "Unfollow";
$subscribe_text = "Follow";
}
编辑:
因此,这是您正在使用的表单代码:
<?php if (isset($_SESSION[user_id]) && $_SESSION[user_id] != $user_id) { ?>
<form action="" method="post">
<input name="action" type="hidden" value="<?php echo $subscribe_status; ?>"/>
<button name="subject" type="submit" value="<?php echo $subscribe_text.' '.$username; ?>">
<?php echo $subscribe_text.' '.$username; ?></button>
</form>
<?php } ?>
基于此,我会改变
<form action="" method="post">
到
<form action="handler.php" method="post">
然后使用以下命令创建一个处理程序文件:
<?
if ($_POST['action'] == "Follow")
//Perform Unfollow Query to Database
else
//Perform Follow Query to Database
?>
那是准系统......但它应该可行。怎么样?