【问题标题】:php left join with if else to show particular html button tagphp left join with if else 以显示特定的 html 按钮标签
【发布时间】:2020-03-14 05:31:22
【问题描述】:

您好,我正在开发类似于 facebook 和 instgram 的社交媒体项目。这里我有 2 个表,一个是用户表,另一个是好友列表,在用户表中,我有所有注册过详细信息的用户,在好友列表中,我有一对多的好友请求关系。一切工作正常。现在我的条件是如果两个朋友都已经是朋友,那么在朋友列表中显示发送消息,否则如果没有朋友则显示添加朋友。 (要检查是否有朋友,我在 mysql db 中有列作为接受)

这是我的好友列表的数据库结构:-

 friend_id | userid | friendId | accept | response |
 +-----------+--------+----------+--------+----------+
 |         1 |      4 |        1 |      1 |        1 |
 |         2 |      5 |        1 |      1 |        1 |
 |         3 |      2 |        4 |      1 |        1 |

这是用户表:-

id | nickname      | email                 | ph_num  | password                         | 
profile_pic                      | status | Created_at          |
+----+---------------+-----------------------+---------+----------------------------------+- 
---------------------------------+--------+---------------------+
|  1 | prasanna      | prasanna@gmail.com    | 12345   | 0a2b75a016a758e08888d66ba244126f | 
1583493901_2020-02-14-110313.jpg |      1 | 2020-03-05 12:52:37 |
|  2 | demo          | demo@d.d              | 1234    | fe01ce2a7fbac8fafaed7c982a04e229 | 
NULL                             |      0 | 2020-03-05 14:15:51 |
|  4 | test          | test@g.g              | 56456   | 098f6bcd4621d373cade4e832627b4f6 | 
1583494768_pic.png               |      0 | 2020-03-06 16:29:43 |
|  5 | one_more_user | onemoreuser@gmail.com | 9988776 | 57bc707659ecf8998c9251b3aae18bd0 | 
NULL                             |      0 | 2020-03-07 14:00:26 |

尝试查询:

$result = "SELECT u.*, f.friendId, f.userid, f.accept FROM Users u LEFT JOIN friendlist f ON 
u.id = f.userid";
$userlist = mysqli_query($conn, $result);

在while循环中:

 <?php
       while ($list = $userlist->fetch_assoc()):?>
        <?php if ($_SESSION['id'] !== $list['id']):?>
        <li class="list-group-item"  >  
       <img class="onlineimg" src="../uploads/<?php echo $list['profile_pic']; ?>"/>
        <?php echo $list['nickname']; ?>
        <?php if ($_SESSION['id'] == $list['friendId']):?>
            <?php if ($list['accept'] ==  '0'):?>
        <p style="display: none" class="selected"><?php echo $list['id']; ?> </p>
        <button class="btn btn-success float-right"><span style="display: none"><?php echo 
       $list['id'] ?> </span> add friend</button>
       <?php endif ;?>
       <?php endif ;?>
        <?php if ($list['accept'] ==  '1'):?>
        <p style="display: none" class="selected"><?php echo $list['id']; ?> </p>
        <button class="btn btn-success float-right"><span style="display: none"><?php echo 
       $list['id'] ?> </span> send message</button>
       <?php endif ;?>
      </li>
    <?php endif ;?>
      <?php endwhile;?>

demo image

从上图中,用户演示显示的是发送消息而不是添加朋友,因为演示不是 prasanna 的朋友。

请帮我解决这个问题,我从过去几个小时就卡在这里了。谢谢你提前,对不起我的英语语法。

【问题讨论】:

  • 如果几乎​​每一行都必须从 html 切换到 php,那么留在 php 中并使用 echo 会更干净。我发现你的脚本在哪里被破坏或者你的确切结果集应该是什么都不是很清楚。如果这是一个 mysql 问题,我们不需要看到“in while loop”sn-p。请隔离问题,仅此而已。
  • 是的,先生。但我已经从引导程序中获取了 css 样式,其中包括特殊字符,例如 - 和所有,并且 php echo 不允许它通过语法错误错误,所以我正在使用这种方式。
  • 一个 db-fiddle.com 演示链接将帮助志愿者(尤其是像我这样主要通过手机做志愿者的人)尝试帮助您。
  • 好吧,让我试试 db-fiddle.com
  • 在执行此操作时,请从架构中删除不必要的列,例如 password。我们正在寻找minimal reproducible example,因为meta.stackoverflow.com/q/333952/2943403

标签: php mysql if-statement left-join


【解决方案1】:

根据你的代码逻辑,唯一可能的帮助是添加 cmets:-

if(session_id == friendID)
{
   if(accept == 0)
  {
    // add friend
  } //you are missing else condition here "what if accept == 1"
} //you are missing else condition here "what if session_id != friendID
//and code below does not belong here at all
 if(accept == 1)
  {
    // send message
  }

【讨论】:

  • 嗨桑迪普,谢谢您的回复。在我的代码中,我使用了两个 if 语句,因为我在 html 标签之间编写了 php 代码。问题是。如果我使用你的方法,我将无法应用 css 类。但谢谢你的回答。
  • 你能告诉我你的要求吗,我已经回答了。暂时忘记 html。
猜你喜欢
  • 2011-10-28
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 2021-12-21
  • 2013-11-02
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多