【问题标题】:PHP MYSQL: how to display chat room messages based on ignore listPHP MYSQL:如何根据忽略列表显示聊天室消息
【发布时间】:2015-06-24 08:25:52
【问题描述】:

我有一个基本的 php 聊天室。每个“房间”都有自己的桌子。在每个房间的桌子上,张贴了单独的消息,每条消息一行。列如下:

id, sender, message

在一个名为 ignore_lists 的单独表中,用户在聊天中有一个命令,可以将用户添加到他们的忽略列表中以用于私人消息目的。效果很好,我已经把那部分完全工作了。

但今天我决定尝试让它也阻止他们在“主屏幕”公共聊天中看到他们忽略列表中的人的消息。

我已经尝试过,但如果不做各种时髦的事情就无法让它工作。

这是显示我的聊天消息的功能:

function showroom($room, $len, $link){
  $query3 = "SELECT * FROM(SELECT `message`, `id`, `sender` FROM `$room` ORDER BY `id` DESC LIMIT $len) AS `id` ORDER BY `id` ASC";
  $result3 = mysqli_query($link, $query3); // execute the query
  if (mysqli_num_rows($result3) > 0) {
    while($row = mysqli_fetch_assoc($result3)) {
      $showmsg = htmlspecialchars_decode($row["message"]);
      echo "$showmsg<br>";
    }}}

如何使用忽略列表表格以某种方式从上述函数中提取消息,但排除出现在忽略列表中的任意数量的人?

供进一步参考.. 忽略列表表只是所有用户的一张大表。每次用户将某人添加到他们的忽略列表时,它都会在 ignore_list 表中插入一行,其中包含以下列:
id, person_being_ignored, name_of_ignore_list_owner

我发表了这样的声明,但没有奏效。或者更确切地说,我应该说它确实有效,但只有当您在忽略列表中只有一个人时它才有效。

query = "SELECT * FROM `ignore_lists` WHERE `ignore_owner`='$id' ORDER BY `person_being_ignored` ASC";
$result = mysqli_query($link, $query); // execute the query

if (mysqli_num_rows($result) > 0) {

  while($mrow = mysqli_fetch_array($result)) {

    $ignoree_name = $mrow['name_of_ignored'];

    $query3 = "SELECT * FROM `$room` WHERE sender <> '$ignoree_name'";
    // $query3 = "SELECT * FROM(SELECT `message`, `id`, `timestamp`, `color` FROM `$room` WHERE sender <> '$ignoree_name' ORDER BY `id` DESC LIMIT $len) AS `id` ORDER BY `id` ASC";
    $result3 = mysqli_query($link, $query3); // execute the query
  }
  while($row = mysqli_fetch_array($result3)) {
    $showmsg = htmlspecialchars_decode($row["message"]);
    echo "$showmsg<br>";
  }

} elseif(mysqli_num_rows($result) < 1) {

  $query3 = "SELECT * FROM(SELECT `message`, `id`, `timestamp`, `color` FROM `$room` ORDER BY `id` DESC LIMIT $len) AS `id` ORDER BY `id` ASC";
  $result3 = mysqli_query($link, $query3); // execute the query

  if (mysqli_num_rows($result3) > 0) {
    while($row = mysqli_fetch_assoc($result3)) {
      $showmsg = htmlspecialchars_decode($row["message"]);
      echo "$showmsg<br>";
    }}}}

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    我们为什么不使用 WHERE NOT IN SQL 语句...

    $ignoree_name = [];
    while($mrow = mysqli_fetch_array($result)) {
        $ignoree_name[] = $mrow['name_of_ignored'];
    }
    $query3 = "SELECT * FROM(SELECT message, id, timestamp, color FROM $room WHERE sender NOT IN ( '" . implode($ignoree_name, "', '") . "' ) ORDER BY id DESC LIMIT 99) AS id ORDER BY id ASC";
    $result3 = mysqli_query($link, $query3); // execute the query
    

    警告...我还没有测试过...

    【讨论】:

    • 谢谢。我还需要几个小时才能回家试一试。但我一试就会告诉你。我不知道我可以在 mysql 语句中使用数组。
    • 它不是一个数组,我使用 implode 来连接数组元素并将结果作为字符串返回,以便在 MySQL 语句中使用。更多在这里php implode
    • 这个半工作。我不得不修改它。它不会在查询中使用 ($wherein) 返回有效的 mysql 结果。我必须这样做 $query3 = "SELECT * FROM(SELECT message, id, timestamp, color FROM $room WHERE sender NOT IN ( '" . implode($ignoree_name, "', '") . "' ) ORDER BY id DESC LIMIT 99) AS id ORDER BY id ASC"; 以防万一你想编辑你的答案
    • 是的,我忘记报价了。
    猜你喜欢
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多