我目前正处于社交网站的中期开发阶段。我试图隐藏和显示基于条件设置的按钮,但我之前遇到了困难。目前,我的想法是无法处理如何将其指定为代码。
我有一个Add to Favourites
在登录用户的个人资料页面之外的页面上可见。让我们假设我以Freddy身份登录,然后我转到Alice的个人资料页面,其中按钮可见,并且为按钮指定的操作工作,数据被发送到数据库。但是,我试图指定一旦用户已经登录用户收藏夹,Add to Favourites
按钮将被Remove from Favourites
替换。
我首先无法指定用户只能对用户进行一次收藏,此时,当在任何配置文件上单击Add to Favourites
时,数据库将更新为多行,当Freddy应该只允许爱丽丝一次。
这是我目前尝试过的,只允许用户使用一次:
<?php
// code to process a favourite request
if (isset($_POST['addfriend'])) {
$fav_request = $_POST['addfriend'];
$favourited_who = $user; // u variable
$favourited_by = $username; // logged in user
/*
* Need to check that the user who is logged in, hasnt already got the $user in there favs
*/
// Step 1: Get all the favourites of the logged in user...
$q = mysqli_query ($connect, "SELECT * FROM favourites");
while ($r_query = mysqli_fetch_array($q)) {
$db_fav_who = $r_query['favourited_who'];
$db_fav_by = $r_query['favourited_by'];
// Step 2: Check whether the profile page the user is viewing, isnt already in there favourites...
// Check: See if $user isn't already in there favourites.
if ($db_fav_by == $username){ // check to see which users favourites we are checking
// 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')");
echo "done ";
}
} // if isset closed
?>
目前,有两个按钮在进入任何人(除了你自己的)个人资料页面时都可见:
<div class="edit-profile">
<?php
if ($user == $username){
// dont display buttons
}else { ?>
<form action="" method="post">
<?php
echo "
<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>";
}
?>
</div>
注意:$user
是获取网址中u变量的变量。 $username
是登录用户的会话变量。
如果{/ 1}}按钮不在登录用户变量中,我怎么才能看到它?如果是,我如何才能看到Add to Favourites
按钮而不是Remove from Favourites
按钮?