我需要一个按钮来替换另一个按钮,具体取决于多个PHP if statements
。它的工作方式是,用户只能favourite
另一个用户一次,如果用户已经受宠,它将显示另一个按钮,而不是默认的一组按钮,这些按钮可供非受欢迎的用户使用。
我的数据库中有一个名为favourites
的表。我已插入一行进行测试。
id - 16
favourited_who - Freddy
favourited_by - AliceP
仅供参考,我的网站目前有三位用户:AliceP
,Freddy
和Fred
。只是为了解释一下,我以[{1}}身份登录,并且您可以看到AliceP
不在我的Fred
中,在这种情况下,应该为favourites
显示两个按钮是Fred
和Send Message
。另一方面,Add to Favourites
在我的收藏夹中,在这种情况下,我想显示两个按钮,Freddy
和Send Message
。
使用下面的代码,Remove from Favourites
和Remove from Favourites
同时显示Fred
按钮。
我需要在我的代码中涵盖这三个条件,我相信我有:
Freddy
以下是我的尝试:
// 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>";
}
闭括号外添加以下内容:
if isset
答案 0 :(得分:0)
你需要一个
public static double value(char[][] array, int x, int y) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == (char) 120) {
int x_cord = i;
int y_cord = j;
int width = (x_cord - x);
int height = (y_cord - y);
Math.abs(width);
Math.abs(height);
double distance = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
return distance;
}
}
}
return 0; // Or some other default
}
以下一行?
<form method='post'>
另一个有他们。
答案 1 :(得分:0)
你写 while loop closed 但是在哪里循环???
可能是
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>";
}
}*/