私がやろうとしているのは、ユーザー フォロー システム (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
があります(クエリで確認してください)。
また、現在ログインしているユーザー ( ) が既にそのユーザーをフォローしているかどうかに応じてorにする$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";
}