За последние 24 часа нас посетили 44977 программистов и 6346 роботов. Сейчас ищут 1863 программиста ...

сообщения привязаны к Ид

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

  1. Сергей1231

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

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

    Вложения:

    • IMG_6461.zip
      Размер файла:
      485,1 КБ
      Просмотров:
      2
  2. MouseZver

    MouseZver Суперстар

    С нами с:
    1 апр 2013
    Сообщения:
    7.840
    Симпатии:
    1.338
    Адрес:
    Лень
    а написать код в 149 строк не составило труда, да ? o_O