私はユーザのためにPHPスクリプトをフォローしていますが、プロフィールユーザページでフォローボタンに問題があるようです。私がサイトに登録されている唯一のユーザーであれば、次のボタンは1つだけ表示されますが、1人または複数のユーザーをサイトに登録すると、このスクリーンショットのように、私はプロフィールページで1つしかフォローしたくありません。どんな助けもありがたい。ありがとう!PHPはユーザーにシステムの問題に従いますプロフィールユーザごとに1つのフォローボタンを表示
Dbの構造:
プロフィールのユーザーの問題を持つページボタン:
これは私がプロフィールページに挿入したPHPコードであります
$get = new Main;
@$user_id = $_SESSION['user'];
$users = $get->users($user_id);
foreach ($users as $row) {
echo ' '.(($row['receiver'] === $_GET['u'] && $row['sender'] === $user_id) ? '<div class="btn btn-success follow following" style="margin-top: -6px;" rel="'.$_GET['u'].'">Following</div>':' <div class="btn btn-danger follow" style="margin-top: -6px;" rel="'.$_SESSION['user'].'">Follow</div>').'';
}
メインPHPスクリプト
class Main{
//get all users from database where user_id not = your id
public function users($user_id){
global $pdo;
$query = $pdo->prepare("SELECT * FROM `mls_users` U LEFT JOIN `follow` F on `f`.`receiver` = `U`.`id` AND CASE WHEN `F`.`sender` = ? THEN `F`.`receiver` = `U`.`id` END where `U`.`id` != ?");
$query->bindValue(1,$user_id);
$query->bindValue(2,$user_id);
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
//this is our follow method
public function follow($user_id,$follow_id){
global $pdo;
//insert into follow where user_id = you and follow_id is = follower
$query = $pdo->prepare("INSERT INTO `follow` (`sender`, `receiver`) VALUES (?, '".$_GET['u']."') ");
//bind $user_id
$query->bindValue(1,$user_id);
//bind $follow_id
//run query
$query->execute();
//add 1+ to follower profile
$this->addNum($follow_id);
}
public function unFollow($user_id,$follow_id){
global $pdo;
//delete user_id and follow_id from follow
$query = $pdo->prepare("DELETE FROM `follow` WHERE `sender` = ? and `receiver` = ?");
//bind user_id
$query->bindValue(1,$user_id);
//bind follow_id
$query->bindValue(2,$follow_id);
//run query
$query->execute();
//add -1 to follower_count
$this->removeNum($follow_id);
}
public function addNum($follow_id){
global $pdo;
//add 1 more num to follow_counter
$query = $pdo->prepare("UPDATE `mls_users` SET `followers_count` = `followers_count` +1 WHERE `id` = '".$_GET['u']."' ");
//bind follow_id
$query->bindValue(1,$follow_id);
//run query
$query->execute();
}
public function removeNum($follow_id){
global $pdo;
//remove 1 num from follow_counter
$query = $pdo->prepare("UPDATE `mls_users` SET `followers_count` = `followers_count` -1 WHERE `id` = ? ");
//bind follow_id
$query->bindValue(1,$follow_id);
//run query
$query->execute();
}
public function getFollowedByUser($follow_id)
{
global $pdo;
$query = $pdo->prepare("SELECT `sender` FROM `follow` where `receiver` = ?");
$query->bindValue(1,$follow_id);
$query->execute();
$followed = $query->fetchAll(PDO::FETCH_COLUMN, 0);
//in case of failure you can check is $followed an array and do something if it's not
if (is_array($followed)) {
return $followed;
}
return array();
}
}
ご回答いただきありがとうございます@giaはコードと変更をお願いします。私はこれに新しいです、おかげで – Zeu
あなたも助けになるコードを掲載havent、私はあなたがやっているべきか、あなたの論理ミスを説明する必要がありますかなりの擬似コードを投稿するあなたのコードを修正することはありません。現時点では、クエリが返されている$ユーザーの数とその理由を確認する必要があります。なぜなら、複数のクエリを返すべきではないということです。 – gia
クエリはユーザーdbからすべてのユーザーを返しています。唯一のユーザーであれば私には1つのみ表示され、OKですが、1人または2人のユーザーを登録すると、ユーザーが登録するたびに問題は私が思うのは$ query = $ pdo-> prepare(select .. from main php – Zeu