0

私の最後の質問は非常に漠然としていたので、いくつかのことを明確にすることにしました。プライベート チャット アプリケーションの作成方法を学んでおり (教育上の理由のみ)、プライベート チャットを行う最善の方法を見つけようとしています。2 つのテーブルを作成しましuserchat。にはchat、5 つの列があります。

  • message_id
  • ユーザーID
  • ユーザー名
  • メッセージ
  • 送信

選択した からメッセージのみを取得したいuser_id

今、ここに私の送信フォームがあります。入力名 send = 取得したいデータ。user_idコツをつかむまでは、番号だけを取得したい(そのため、そのユーザー名とチャットするには、テキスト領域に番号を入力します)。

<form name="form" method="post">
    <input type="text" name="send" id="send" value="">
    <input name="user_id" type="hidden" value="<?php echo $_SESSION['user_id'];?>" id="user_id">
    <textarea name="message" class="message" onkeydown="if (event.keyCode == 13) document.getElementById('submit').click()"></textarea>
    <input type="submit"  name="Submit" id="submit" value="">
</form>   

そして、ここに私のphp検索フォームがあります

<?php
class Chat extends Core {
   public function fetchMessages() {
        $this->query("
        SELECT          chat.message,
                        users.username,
                        users.user_id,
                        chat.send
        FROM            chat
        JOIN            users
        ON              chat.user_id //and chat.send = users.user_id 
        ORDER BY        chat.timestamp
        DESC
    ");

    return $this->rows();
   }

}

基本的に、次のような条件を設定したいと思います。

<?php
$send=$_POST['send'];
$userid=$_POST['user_id'];
if ($send == $userid || (isset($_POST['user_id']))) //this isn't correct- just trying to    display my though
{
    //retrieve message from user_id number in sql table, allowing the people that have the same user_id as send to only read the message.
}   
?>

基本的に、特定の user_id 番号の情報のみを取得するにはどうすればよいですか? また、この条件をどこに配置すればよいでしょうか?

4

1 に答える 1

0

何を言っているのか正確にはわかりませんが、特定のユーザーIDからメッセージを取得したい場合は、次のようにすることができます:

if(isset($_POST['getMsgsById']) && isset($_POST['userid'])){
  $userid = (int)$_POST['userid'];

  $chat = new Chat(); // 


  if(!$msgs = $chat->getMsgsByUserId($userid)){
       echo "No messages found";
  }else{
      $length = count($msgs);

      for($i=0;$i<$length;$i++){
          echo $msgs[$i]['msg']. '<br />';
      }

  }
}


class Chat{

   public function getMsgsByUserId($id){
       $result = mysql_query("SELECT * FROM your_message_table WHERE userid=$id");
       if(mysql_num_rows($result) == 0)
            return array();

       while($info = mysql_fetch_assoc($result)){
           $arr[] = $info;
       }

       return $arr;
   }

}

もちろん、これは非常に高速なものです。

于 2013-09-12T23:59:07.527 に答える