简体   繁体   English

如果似乎不起作用

[英]IF doesn't seem to be working

Right, I am going to get straight into this. 是的,我将直接讲这个。

I am building an Add Friend button. 我正在建立一个“添加朋友”按钮。 if the person is in the same row as the session (person logged on), it shouldn't echo Add Friend. 如果此人与会话(登录的人)在同一行,则不应回显“添加朋友”。

If the name of the person is in the row, and the page id doesn't match the session id, it should echo a button. 如果此人的姓名在该行中,并且页面ID与会话ID不匹配,则应回显一个按钮。 But there are two rows the name might be in and I use a simple '||' 但是名称可能有两行,我使用了一个简单的“ ||” to seperate. 分开。 But my if doesn't work. 但是我的if不起作用。 It returns 'Add Friend' despite the fact that it is in the last. 尽管它在最后一个,但它仍返回“添加朋友”。 I deleted the second 'OR' factor and just used one statement and it worked. 我删除了第二个“ OR”因子,只使用了一条语句就可以了。

RelatedUser and RelatingUser are the two possible fields that page id result might be in. RelatedUser和RelatingUser是页面ID结果可能位于的两个可能字段。

My code is below: 我的代码如下:

    $session_username_query = mysql_query("SELECT * FROM users WHERE username='{$_SESSION['user_login']}'");
    $session_username_assoc = mysql_fetch_assoc($session_username_query);
    $session_username = $session_username_assoc['username']; //the person who is logged in

    $profile_view_query = mysql_query("SELECT * FROM friend WHERE RelatedUser='$session_username' OR RelatingUser='$session_username'");
     $profile_view_assoc = mysql_fetch_assoc($profile_view_query);
     $related = $profile_view_assoc['RelatedUser'];
     $relating = $profile_view_assoc['RelatingUser']; 

      if ($username != $relating && $_GET['id'] != $session_username_assoc['id'] || $username != $related && $_GET['id'] != $session_username_assoc['id']) {
 $addAsFriend = '<input type="submit" name="addfriend" value="Add Friend">';
    echo $addAsFriend;
    } 

I think removing the OR operand (||) would give you the desired result. 我认为删除OR操作数(||)将为您提供所需的结果。

if (
    $username != $relating &&
    $username != $related &&
    $_GET['id'] != $session_username_assoc['id']
) {
    $addAsFriend = '<input type="submit" name="addfriend" value="Add Friend">';
    echo $addAsFriend;
} 

Edit 编辑

Perhaps I misread your question. 也许我误解了你的问题。 Does this work instead? 这样做有效吗?

if (
    ($username != $relating || $username != $related) &&
    $_GET['id'] != $session_username_assoc['id']
) {
    $addAsFriend = '<input type="submit" name="addfriend" value="Add Friend">';
    echo $addAsFriend;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM