За последние 24 часа нас посетил 85321 программист и 4735 роботов. Сейчас ищут 1813 программистов ...

как к текстовому чату добавить обмен изображениями ?

Тема в разделе "PHP для новичков", создана пользователем Сергей1231, 26 мар 2023.

  1. Сергей1231

    Сергей1231 Новичок

    С нами с:
    20 фев 2023
    Сообщения:
    5
    Симпатии:
    0
    PHP:
    1. <?php
    2.  
    3. require_once("./Connector/DbConnectorPDO.php");
    4. require("./helper/helperFunctions.php");
    5. $userId = isset($_SESSION["userId"]) && !IsVariableIsSetOrEmpty($_SESSION["userId"]) ? $_SESSION["userId"] : 0;
    6. $connection = getConnection();
    7. $userObj = $userId !== 0 && !IsVariableIsSetOrEmpty($_SESSION["user"]) ? $_SESSION["user"] : "";
    8. $msgList = [];
    9. $recentMsgList = [];
    10. $msgToUserId = isset($_GET["id"]) && !IsVariableIsSetOrEmpty($_GET["id"]) ? $_GET["id"] : 0;
    11.  
    12.  
    13.  
    14. if (isset($_POST["SendMessage"]) && !IsVariableIsSetOrEmpty($_POST["SendMessage"])) {
    15.     $msg = $_POST["msg"]; {
    16.      
    17.        header("Location: " . $_SERVER["REQUEST_URI"]);
    18.      
    19.  
    20.     }
    21.  
    22.  
    23.  
    24.        
    25.  
    26.     if (isset($msg) && !IsVariableIsSetOrEmpty($msg)) {
    27.         $insertMessageQuery = "INSERT INTO messages(msg_from_user_id,msg,msg_to_user_Id,msg_date,is_msg_read)
    28.                               values(:userId,:msg,:msgToUserId,NOW(),0)";
    29.         $insertStmt = $connection->prepare($insertMessageQuery);
    30.         $insertStmt->bindParam(':userId', $userId);
    31.         $insertStmt->bindParam(':msg', $msg);
    32.         $insertStmt->bindParam(':msgToUserId', $msgToUserId);
    33.         $insertStmt->execute();
    34.    
    35.     }
    36.  
    37. }
    38.  
    39. if ($userId === 0 || (!isset($_GET["id"]) && !isset($msgTouserId))) {
    40.            
    41.      
    42.  
    43. }
    44.  
    45. if ($msgToUserId !== 0) {
    46.  
    47.     $recentMsgQuery = "SELECT
    48.    *
    49. FROM
    50.    (
    51.    SELECT profile
    52.        .id,
    53.        profile.firstName,
    54.        profile.lastName,
    55.        profile.imgUrl,
    56.        (
    57.        SELECT
    58.            msg
    59.        FROM
    60.            messages
    61.        WHERE
    62.            messages.msg_from_user_id = profile.id OR messages.msg_to_user_id = profile.id
    63.        ORDER BY
    64.            id
    65.        DESC
    66.    LIMIT 1
    67.    ) AS lastMessage,
    68.    (
    69.    SELECT
    70.        msg_date
    71.    FROM
    72.        messages
    73.    WHERE
    74.        messages.msg_from_user_Id = profile.Id OR messages.msg_to_user_Id = profile.Id
    75.    ORDER BY
    76.        id
    77.    DESC
    78. LIMIT 1
    79. ) AS msgDate
    80. FROM profile
    81. WHERE
    82.    id <> :userId
    83. ) X
    84. LEFT JOIN (
    85.    SELECT DISTINCT ids
    86.    from (
    87.            select msg_from_user_id as ids
    88.            from messages
    89.            where msg_from_user_id =:userId  or msg_to_user_id=:userId
    90.            UNION
    91.            select msg_to_user_id as ids
    92.            from messages
    93.            where msg_from_user_id =:userId  or msg_to_user_id=:userId
    94.        )uniqueIdList
    95.        where ids <> :userId
    96.   )IdList on X.id=IdList.ids
    97. WHERE
    98.    ids IS NOT NULL
    99.    AND
    100.    lastMessage IS NOT NULL
    101.    ORDER BY msgDate desc";
    102.     $recentQueryStmt = $connection->prepare($recentMsgQuery);
    103.     $recentQueryStmt->bindParam(':userId', $userId);
    104.     $recentQueryStmt->execute();
    105.     $recentMsgList = $recentQueryStmt->fetchAll();
    106.    
    107.  
    108.  
    109.     $query = "SELECT m.id as msg_id
    110.      ,msg_from_user_id
    111.      ,msg_to_user_id
    112.      ,msg
    113.      ,msg_date
    114.      ,is_msg_read
    115.      ,msg_read_date
    116.      ,fromUser.id as fromUserId
    117.      ,fromUser.firstName as fromFirstName
    118.      ,fromUser.lastName as fromLastName
    119.      ,fromUser.imgUrl as fromUserImgUrl
    120.      ,toUser.id as toUserId
    121.      ,toUser.firstName as toUserFirstName
    122.      ,toUser.lastName as toUserLastName
    123.      ,toUser.imgUrl as toUserImgUrl
    124. FROM messages m
    125. LEFT JOIN profile as fromUser on m.msg_from_user_id=fromUser.id
    126. LEFT JOIN profile as toUser on m.msg_to_user_id=toUser.id
    127. WHERE (msg_from_user_id =:userId and msg_to_user_id=:sentToUserId) or (msg_from_user_id=:sentToUserId and msg_to_user_id=:userId)";
    128. //ORDER BY msg_date DESC";
    129.  
    130.     $stmt = $connection->prepare($query);
    131.     $stmt->bindParam(':userId', $userId);
    132.     $stmt->bindParam(':sentToUserId', $_GET["id"]);
    133.     $stmt->execute();
    134.     $msgList = $stmt->fetchAll();
    135.    
    136.  
    137.     if (count($msgList) > 0) {
    138.         $lastMsgRow = end($msgList);
    139.         if ($lastMsgRow["msg_from_user_id"] === $msgToUserId && $lastMsgRow["msg_to_user_id"] === $userId && intval($lastMsgRow["is_msg_read"]) === 0) {
    140.             $updateAllMsgReadQuery = "UPDATE messages set is_msg_read=1,msg_read_date=NOW() where msg_from_user_id=:sentToUserId and msg_to_user_id=:userId and is_msg_read=0";
    141.             $stmt = $connection->prepare($updateAllMsgReadQuery);
    142.             $stmt->bindParam(':sentToUserId', $msgToUserId);
    143.             $stmt->bindParam(':userId', $userId);
    144.             $stmt->execute();
    145.            
    146.         }
    147.     }
    148. }
    149.  
    150.  
    151. ?>
    HTML:
    1. <div class="type_msg">
    2.                             <div class="input_msg_write">
    3.                                 <form action="chat-users.php?id=<?= $msgToUserId ?>" method="post">
    4.                                     <input type="text" name="msg" class="write_msg" placeholder="Введите сообщение"
    5.                                           required/>
    6.                                     <button id="sendBtn" class="msg_send_btn" type="button">
    7.                                         <i class="fa fa-paper-plane-o" aria-hidden="true"></i>
    8.                                     </button>
    9.                                     <input type="Submit" id="sendMessage" style="display: none;" value="Send"
    10.                                           name="SendMessage"/>
    11.                                 </form>
    12.                             </div>
    13.                         </div>
     
  2. ADSoft

    ADSoft Старожил

    С нами с:
    12 мар 2007
    Сообщения:
    3.874
    Симпатии:
    756
    Адрес:
    Татарстан
    Молча - как правило тегами, либо отдельным типом сообщения