PHP会跟踪用户系统问题,并为每个配置文件用户显示一个关注按钮

时间:2016-08-10 00:22:42

标签: php

我为我的用户提供了一个跟踪PHP脚本并且工作正常,但在配置文件用户页面上,关注按钮似乎有一些问题。如果我是在网站上注册的唯一用户,它看起来很好,只有一个跟随按钮,但如果我向网站注册一个或多个用户,则跟随按钮在页面上克隆自己,就像在此屏幕截图中,有3个以下按钮我想在个人资料页面上只关注一个。任何帮助赞赏。谢谢!

Db结构:

enter image description here

使用以下问题配置用户页面按钮:

enter image description here

这是我在个人资料页面中插入的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();
}
}

1 个答案:

答案 0 :(得分:1)

您正在打印每行$ user失败的行

$row['receiver'] === $_GET['u'] && $row['sender'] === $user_id

第一个建议,使用&#34; if&#34;为了提高代码的可读性,代码按原样填充在一行中的字符太多了。

其次,由于这可能是单个用户的配置文件,因此对于每个用户,请跳过,直到找到要为其显示数据的ID(运行继续;否则)。

之后,打印&#34;已经跟随&#34;如果正在遵循,否则打印跟随框。

第三,它可能是您的个人资料,该功能旨在为您提供按钮,以跟踪数据库中的每个其他用户。然后编辑Follow to Follow Username。由于你没有提供html,我只能猜测代码的意图。