按钮,根据PHP查询替换另一个按钮
我需要一个按钮来替换另一个按钮,具体取决于几个按钮,根据PHP查询替换另一个按钮,php,Php,我需要一个按钮来替换另一个按钮,具体取决于几个PHP if语句。它的工作方式是,一个用户只能收藏另一个用户一次,如果一个用户已经被收藏,它将显示另一个按钮,而不是非收藏用户可用的默认按钮集 我的数据库中有一个名为收藏夹的表。我插入了一行用于测试 id - 16 favourited_who - Freddy favourited_by - AliceP 仅供参考,我的网站目前有三个用户:AliceP、Freddy和Fred。只是为了解释一下,我是以AliceP的身份登录的,并且正如您所看到的F
PHP if语句
。它的工作方式是,一个用户只能收藏
另一个用户一次,如果一个用户已经被收藏,它将显示另一个按钮,而不是非收藏用户可用的默认按钮集
我的数据库中有一个名为收藏夹
的表。我插入了一行用于测试
id - 16
favourited_who - Freddy
favourited_by - AliceP
仅供参考,我的网站目前有三个用户:AliceP
、Freddy
和Fred
。只是为了解释一下,我是以AliceP
的身份登录的,并且正如您所看到的Fred
不在我的收藏夹中
,在这种情况下,应该为Fred
显示两个按钮,分别是发送消息
和添加到收藏夹
。另一方面,Freddy
在我的收藏夹中,在这种情况下,我想显示两个按钮,分别是Send Message
和Remove from favorities
下面的代码显示了Fred
和Freddy
的Remove from favorities
按钮
我需要在我的代码中包含三个条件,我相信我有:
// 1. Con 1: Dont display any buttons for the logged in user as they cannot fav or msg themselves.
// 2. Con 2: Display sendmsg and addfriend btn if the user is not in the logged in users favs
// 3. Con 3: Display sendmsg and remfriend btn is the user is in the logged in users favs.
以下是我尝试过的:
if (isset($_POST['addfriend'])) {
$fav_request = $_POST['addfriend'];
$favourited_who = $user; // u variable
$favourited_by = $username; // logged in user
// Get all the favourites of the logged in user...
$q = mysqli_query ($connect, "SELECT * FROM favourites WHERE favourited_by='$username'");
$num_of_favs = mysqli_num_rows($q);
$r_query = mysqli_fetch_array($q);
$db_fav_who = $r_query['favourited_who'];
$db_fav_by = $r_query['favourited_by'];
// Check: See if $user isn't already in there favourites.
/*if ($db_fav_by == $username){ // if logged in user has anyone favourited
// if the user already exists in the logged in users favourites, then display remove from favourites button.
if ($db_fav_who == $user){
echo "<div class='edit_profile'>
<input type='submit' class='btn btn-info' name='remfriend' value='Remove from Favourites'>
</div>";
}
}*/
//}// while loop closed
if ($user != $username) { // Check: See user isnt favouriting themself.
$favourite_user = mysqli_query($connect, "INSERT INTO favourites VALUES ('', '$favourited_who', '$favourited_by')");
}
} // if isset closed
// Condition 1:
if ($user == $username){
// dont display buttons
}
// Condition 2:
if ($user != $username && $favourited_who != $db_fav_who){ // if $user is not equal to db fav_who value
echo "<form method='post'>
<input type='submit' class='btn btn-info' name='sendmsg' value='Send Message'/>
<input type='submit' class='btn btn-info' name='addfriend' value='Add to Favourites'>
</form>";
}
// Condition 3:
if ($user != $username && $favourited_who == $db_fav_who) {
echo "<form method='post'>
<input type='submit' class='btn btn-info' name='sendmsg' value='Send Message'/>
<input type='submit' class='btn btn-info' name='remfriend' value='Remove from Favourites'>
</form>";
}
你需要一张支票吗
<form method='post'>
下一行呢
echo "<div class='edit_profile'>
<input type='submit' class='btn btn-info' name='remfriend' value='Remove from Favourites'>
</div>";
echo”
";
另一个有。你需要一个吗
<form method='post'>
下一行呢
echo "<div class='edit_profile'>
<input type='submit' class='btn btn-info' name='remfriend' value='Remove from Favourites'>
</div>";
echo”
";
另一个有它们。你在循环关闭时写,但是循环在哪里 可能是
while($r_query = mysqli_fetch_array($q)) { ... };
您还可以在此代码中注释一个{
和两个}
/* if ($db_fav_who == $user){
echo "<div class='edit_profile'>
<input type='submit' class='btn btn-info' name='remfriend' value='Remove from Favourites'>
</div>";
}
}*/
/*if($db\u fav\u who==$user){
回声“
";
}
}*/
您在循环关闭时编写但循环在哪里
可能是
while($r_query = mysqli_fetch_array($q)) { ... };
您还可以在此代码中注释一个{
和两个}
/* if ($db_fav_who == $user){
echo "<div class='edit_profile'>
<input type='submit' class='btn btn-info' name='remfriend' value='Remove from Favourites'>
</div>";
}
}*/
/*if($db\u fav\u who==$user){
回声“
";
}
}*/
导致该回显的if语句与在//条件3
下找到的代码类似。然而,我希望
替换条件2中的addfriend
按钮输入,该按钮被表单
标记包围。Feddy,这只是一个猜测!,但在前面的if语句中,$db_fav_who和favorite_who的值设置为相同的值。在下面一行$db_fav_who=$r_query['favorited_who'];因此,当继续执行if语句时,它们将匹配,并且满足第三个条件。这是对的还是我遗漏了什么?我第一次给
$db\u fav\u who赋值时,是在“$db\u fav\u who=$r\u query['favorited\u who']”这行这是从数据库中获取“Favorited_who”列的数据。我希望这能回答您的问题。导致该回显的if语句
与//条件3
下的代码类似。然而,我希望
替换条件2中的addfriend
按钮输入,该按钮被表单
标记包围。Feddy,这只是一个猜测!,但在前面的if语句中,$db_fav_who和favorite_who的值设置为相同的值。在下面一行$db_fav_who=$r_query['favorited_who'];因此,当继续执行if语句时,它们将匹配,并且满足第三个条件。这是对的还是我遗漏了什么?我第一次给$db\u fav\u who赋值时,是在“$db\u fav\u who=$r\u query['favorited\u who']”这行这是从数据库中获取“Favorited_who”列的数据。我希望这能回答你的问题。我最初确实有一个while循环,它是while($r\u query=mysqli\u fetch\u array($q)){…}
,但后来我删除了while循环,并注释掉了结束标记。一个}
关闭while循环(我已经删除了)。另一个}
关闭这个if语句如果($db_fav_who==$user){
最后一个}
关闭这个if语句如果($db_fav_by===$username){
我最初确实有一个while循环,它是而($r_query=mysqli_fetch_数组($q)){…}
,但后来我删除了while循环,并注释掉了closing标记。One}
关闭while循环(我已删除)。另一个}
关闭此if语句if($db\u fav\u who==$user){
和最后一个}
关闭此if语句if($db\u fav\u by==$username){
。