PHP帮助:在微博中关注系统

时间:2015-02-24 15:20:44

标签: php css web-site-project

我制作的网站与Instagram类似。我是非常新的PHP。我在用户个人资料中创建了一个关注按钮。

如果您已经关注用户,如何使关注按钮消失? 如何使用取消关注按钮替换它?

//我的php代码,用于跟随

if (isset($_POST['addfriend'])){


    $fromuser = $user;
    $touser = $username;

    if($fromuser == $username){
        $Msg = "You cannot follow yourself<br/>";
    }
    else
    {
        $getID= mysql_query("SELECT userID FROM user WHERE username='$user'");
        $get_ID_row = mysql_fetch_assoc($getID);
        $ID_db = $get_ID_row['userID'];

        $sql = "insert into following (userID, fromUser, toUser) 
        values ('$ID_db','$fromuser', '$touser')";

        $result = mysql_query($sql);

         $Msg= "Success! <br/>";

    }
}
else{
    //Do nothing
}

//我的关注按钮的代码

                        <form action="<?php $user;?>" method ="POST">
                        <?php echo $Msg; ?>
                        <input type = "submit" name ="addfriend" value = "Follow"/>
                        </form>

2 个答案:

答案 0 :(得分:0)

使用OO PHP会更容易。但是,如果您选择程序,我们假设我们有一个朋友表。这保留了我每个朋友的身份。 例如:史密斯跟随约翰

enter image description here

然后你会做类似的事情     $following = mysql_query("SELECT COUNT(*) FROM followers WHERE followerid = ".$_SESSION['id']." AND followeeid = ".$username);

检查您是否已经关注此人: if($following){//$following == true }

答案 1 :(得分:0)

在您要显示“关注”或“取消关注”按钮的页面上,首先运行MySQL查询,以确定您是否已关注此人:

$sql = "select * from following 
        where userID = $user
        and   fromUser = $fromUser
        and   toUser = $toUser";
$result = mysql_query($sql);

if( $result) {
    if( mysql_num_rows($result) > 0) {
        // if we get here we know we are already following that person
        ....[see below]

现在动态创建您需要的任何按钮: -

    if( mysql_num_rows($result) > 0) {
        // if we get here we know we are already following that person
        echo '<input type = "submit" name ="removefriend" value = "Un-follow"/>';
    }
    else
    {
        echo '<input type = "submit" name ="addfriend" value = "Follow"/>';
    }

在接收表单结果的下一页上,检查两个按钮:

if (isset($_POST['addfriend'])) {
    ...[do what you already have]
}
else
if (isset($_POST['removefriend'])) {
    ...[do SQL to remove the record from the following table]
}

请注意,从PHP v5.5开始,这种MySQL风格已被弃用。在将来的某个阶段,您必须将程序转换为MySQLi或PDO_MySQL扩展,然后才能最终停止支持。请参阅PHP手册,例如http://php.net/manual/en/mysqlinfo.api.choosing.php