[英]How to create a user following php script?
我想要做的是在我的腳本中添加一個用戶跟隨系統(twitter like)。
這就是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
)是否已經跟隨該用戶。
有人能幫幫我嗎?
編輯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";
}
這是一些代碼:
$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
?>
這是准系統......但它應該成功。 怎么樣?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.