PHP Loop Issue

Discussion in 'PHP' started by Jeremy Benson, Jan 29, 2023.

  1. #1
    I'm having trouble with fetching some chat logs. I'm getting some repeated code in my output. 'm not sure why. closeForm() and Chat is repeating throughout. It should just be making the messages.

    <?php
    
    session_start();
    $rawData = file_get_contents("php://input");
    $data = [];
    parse_str($rawData, $data);
    $returnMessage = [];
    include "../classes/RandomHelper.php";
    
    if (
        isset($_SESSION["ID"]) &&
        isset($_SESSION["token"]) &&
        isset($_SESSION["serial"])
    ) {
        include __DIR__ . "../../../../Yen7AaH9rb/Xen7AaL9rb.php";
    
        $usersXML = __DIR__ . "../../../../Yen7AaH9rb/database/" . $usersTable;
        $chatLogXML = __DIR__ . "../../../../Yen7AaH9rb/database/" . $chatLogTable;
    
        include "../classes/User.php";
        $userClass = new User();
    
        if (is_file($usersXML)) {
            $userToken = $_SESSION["token"];
    
            $usersDocXML = file_get_contents($usersXML);
    
            if (trim($usersDocXML) == "") {
                $usersDocXML = file_get_contents($usersXML);
    
                if (trim($usersDocXML) == "") {
                    $returnMessage["xmlError"] = "empty document";
                    die(json_encode($returnMessage));
                }
            }
    
            $usersDoc = simplexml_load_string($usersDocXML);
    
            $userToken = $_SESSION["token"];
            $user = $usersDoc->xpath("//users/user[@token='$userToken']");
    
            if (!empty($user)) {
                $userSerial = $user[0]->serial["value"][0]->__toString();
    
                if ($_SESSION["serial"] === $userSerial) {
                    // create chat log, fetch last 12 message
    
                    $chatLogDocXML = file_get_contents($chatLogXML);
    
                    if (trim($chatLogDocXML) == "") {
                        $chatLogDocXML = file_get_contents($chatLogXML);
    
                        if (trim($chatLogDocXML) == "") {
                            $returnMessage["xmlError"] = "empty document";
                            die(json_encode($returnMessage));
                        }
                    }
    
                    $logDoc = simplexml_load_string($chatLogDocXML);
                    // Bug: Remove line $data['member']
                    $data["member"] = "df4d8ca3ba095a9bded14f9e2c307b6e1";
                    $member = $data["member"];
    
                    $logFetch = $logDoc->xpath(
                        "//logs/log[@user1='$userToken' and @user2='$member' or @user2='$member' and @user1 = '$userToken']"
                    );
    
                    $itemsPerPage = 12;
                    $limit = $itemsPerPage - 1;
    
                    $logHolder = [];
    
                    $ID = 1;
                    $messageCount = 0;
    
                    foreach ($logFetch[0]->message as $index => $item) {
                        $logHolder[$ID] = $item;
                        $ID = $ID + 1;
                        $messageCount = $messageCount + 1;
                    }
    
                    $totalPages = ceil($messageCount / $itemsPerPage);
                    $currentPage = $totalPages;
                    $startID = (int) $currentPage * 12 - $limit;
                    $limitID = (int) $currentPage * 12;
    
                    if ($ID > 12) {
                        // collect the last 12 message
    
                        $gatheredMessages = [];
    
                        foreach ($logHolder as $index => $item) {
                            if ($index >= $startID and $index <= $limitID) {
                                array_push($gatheredMessages, $item);
                            }
                        }
    
                        // end collect last 12 messages
                    } else {
                        // send back all message
                        $ID = 1;
    
                        foreach ($logHolder as $index => $item) {
                            $gatheredMessages[$ID] = $item;
                            $ID = $ID + 1;
                        }
                    }
    
                    // create the chat html
                    $html = '<div class="card-header bg-white d-flex justify-content-between align-items-center p-3">
                                                    <h5 class="mb-0">Chat</h5>
                                                            <button type="button" class="btn btn-outline-primary btn-sm" onclick="closeForm()"><i
                                                                class="fa fa-close"></i></button>
                                            </div>
                                            <div class="card-body overflow-auto chat-scrollbar" style="position: relative; height: 400px">';
    
                    foreach ($gatheredMessages as $index => $item) {
                        if ($item["from"] == $_SESSION["token"]) {
                            $html =
                                $html .
                                '<div class="d-flex flex-row justify-content-end mb-4 pt-1">
                                                                <div>
                                                                    <p class="small p-2 me-3 mb-1 text-white rounded-3 bg-primary">' .
                                htmlentities($item["message"]) .
                                '</p>
                                                                    <p class="small me-3 mb-3 rounded-3 text-muted d-flex justify-content-end">' .
                                // time here
    
                                ($html =
                                    $html .
                                    '</p>
                                                                </div>
                                                                <img src="uploads/profile/');
    
                            if (
                                $userClass->returnProfileImage(
                                    $item["from"],
                                    $usersXML
                                ) !== ""
                            ) {
                                $html =
                                    $html .
                                    $userClass->returnProfileImage(
                                        $item["from"],
                                        $usersXML
                                    );
                            }
    
                            $html =
                                $html .
                                '" alt="avatar 1" style="width: 45px; height: 45px;"
                                                                    class="rounded-circle">
                                                            </div>';
                        } else {
                            $html =
                                $html .
                                '<div class="d-flex flex-row justify-content-end mb-4 pt-1">
                                                                <div>
                                                                    <p class="small p-2 me-3 mb-1 text-white rounded-3">' .
                                htmlentities($item["message"]) .
                                '</p>
                                                                    <p class="small me-3 mb-3 rounded-3 text-muted d-flex justify-content-end">' .
                                // time here
    
                                ($html =
                                    $html .
                                    '</p>
                                                                </div>
                                                                <img src="uploads/profile/');
    
                            if (
                                $userClass->returnProfileImage(
                                    $item["to"],
                                    $usersXML
                                ) !== ""
                            ) {
                                $html =
                                    $html .
                                    $userClass->returnProfileImage(
                                        $item["to"],
                                        $usersXML
                                    );
                            }
    
                            $html =
                                $html .
                                '" alt="avatar 1" style="width: 45px; height: 45px;"
                                                                    class="rounded-circle">
                                                            </div>';
                        }
    
                        // end handle messages
                        // end messages loop
                    }
    
                    $html =
                        $html .
                        ' </div>
                                    <div class="card-footer bg-white text-muted d-flex justify-content-start align-items-center p-3">
                                                        <img src="uploads/profile/' .
                        $userClass->returnProfileImage(
                            $_SESSION["token"],
                            $usersXML
                        ) .
                        '" alt="avatar 3" style="width: 40px; height: 40px;" class="rounded-circle">
                                                        <input type="text" class="fs-6 form-control border-0 form-control-lg" id="exampleFormControlInput1"
                                                            placeholder="Type message">
                                                        <a class="ms-1 text-muted" href="#!"><i class="fas fa-paperclip"></i></a>
                                                        <a class="ms-3 text-muted" href="#!"><i class="fas fa-smile"></i></a>
                                                        <a class="ms-3" href="#!"><i class="fas fa-paper-plane"></i></a>
                                                    </div>';
    
                    $returnMessage["message"] = "success";
                    $returnMessage["html"] = $html;
    
                    echo $html;
                    //echo json_encode($returnMessage);
    
                    // end create chat log
                } else {
                    // Bug: bump session
    
                    header("Location: ../page-not-found.html");
                    exit();
                }
            } else {
                // Bug: bump session
    
                header("Location: ../page-not-found.html");
                exit();
            }
        } else {
            header("Location: ../page-not-found.html");
            exit();
        }
    } else {
        header("Location: ../page-not-found.html");
        exit();
    }
    
    ?>
    
    Code (markup):
    xml source

    <?xml version="1.0" standalone="yes"?>
    <logs>
      <log user1="df4d8ca3ba095a9bded14f9e2c307b6e" user2="df4d8ca3ba095a9bded14f9e2c307b6e1">
            <message id="1" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="2" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="3" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="4" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="5" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="6" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="7" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="8" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="9" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="10" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="11" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="12" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="13" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="14" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="15" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="16" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="17" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="18" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="19" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="20" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="21" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="22" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="23" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
            <message id="24" from="df4d8ca3ba095a9bded14f9e2c307b6e" to="df4d8ca3ba095a9bded14f9e2c307b6e1" message="This is a message from a user to another user" />
      </log>
    </logs>
    
    Code (markup):
    snippet of html created

    <div class="card-header bg-white d-flex justify-content-between align-items-center p-3">
      <h5 class="mb-0">Chat</h5>
      <button type="button" class="btn btn-outline-primary btn-sm" onclick="closeForm()">
        <i class="fa fa-close"></i>
      </button>
    </div>
    <div class="card-body overflow-auto chat-scrollbar" style="position: relative; height: 400px">
      <div class="d-flex flex-row justify-content-end mb-4 pt-1">
        <div>
          <p class="small p-2 me-3 mb-1 text-white rounded-3 bg-primary">This is a message from a user to another user</p>
          <p class="small me-3 mb-3 rounded-3 text-muted d-flex justify-content-end">
          <div class="card-header bg-white d-flex justify-content-between align-items-center p-3">
            <h5 class="mb-0">Chat</h5>
            <button type="button" class="btn btn-outline-primary btn-sm" onclick="closeForm()">
              <i class="fa fa-close"></i>
            </button>
          </div>
          <div class="card-body overflow-auto chat-scrollbar" style="position: relative; height: 400px">
            </p>
          </div>
          <img src="uploads/profile/image.png" alt="avatar 1" style="width: 45px; height: 45px;" class="rounded-circle">
        </div>
        <div class="d-flex flex-row justify-content-end mb-4 pt-1">
          <div>
            <p class="small p-2 me-3 mb-1 text-white rounded-3 bg-primary">This is a message from a user to another user</p>
            <p class="small me-3 mb-3 rounded-3 text-muted d-flex justify-content-end">
            <div class="card-header bg-white d-flex justify-content-between align-items-center p-3">
              <h5 class="mb-0">Chat</h5>
              <button type="button" class="btn btn-outline-primary btn-sm" onclick="closeForm()">
                <i class="fa fa-close"></i>
              </button>
            </div>
            <div class="card-body overflow-auto chat-scrollbar" style="position: relative; height: 400px">
              <div class="d-flex flex-row justify-content-end mb-4 pt-1">
                <div>
                  <p class="small p-2 me-3 mb-1 text-white rounded-3 bg-primary">This is a message from a user to another user</p>
                  <p class="small me-3 mb-3 rounded-3 text-muted d-flex justify-content-end">
                  <div class="card-header bg-white d-flex justify-content-between align-items-center p-3">
                    <h5 class="mb-0">Chat</h5>
                    <button type="button" class="btn btn-outline-primary btn-sm" onclick="closeForm()">
                      <i class="fa fa-close"></i>
                    </button>
                  </div>
                  <div class="card-body overflow-auto chat-scrollbar" style="position: relative; height: 400px">
                    </p>
                  </div>
                  <img src="uploads/profile/image.png" alt="avatar 1" style="width: 45px; height: 45px;" class="rounded-circle">
                </div>
                </p>
              </div>
              <img src="uploads/profile/image.png" alt="avatar 1" style="width: 45px; height: 45px;" class="rounded-circle">
            </div>
            <div class="d-flex flex-row justify-content-end mb-4 pt-1">
              <div>
                <p class="small p-2 me-3 mb-1 text-white rounded-3 bg-primary">This is a message from a user to another user</p>
                <p class="small me-3 mb-3 rounded-3 text-muted d-flex justify-content-end">
                <div class="card-header bg-white d-flex justify-content-between align-items-center p-3">
                  <h5 class="mb-0">Chat</h5>
                  <button type="button" class="btn btn-outline-primary btn-sm" onclick="closeForm()">
                    <i class="fa fa-close"></i>
                  </button>
                </div>
                <div class="card-body overflow-auto chat-scrollbar" style="position: relative; height: 400px">
                  <div class="d-flex flex-row justify-content-end mb-4 pt-1">
                    <div>
                      <p class="small p-2 me-3 mb-1 text-white rounded-3 bg-primary">This is a message from a user to another user</p>
                      <p class="small me-3 mb-3 rounded-3 text-muted d-flex justify-content-end">
                      <div class="card-header bg-white d-flex justify-content-between align-items-center p-3">
                        <h5 class="mb-0">Chat</h5>
                        <button type="button" class="btn btn-outline-primary btn-sm" onclick="closeForm()">
                          <i class="fa fa-close"></i>
                        </button>
                      </div>
                      <div class="card-body overflow-auto chat-scrollbar" style="position: relative; height: 400px">
                        </p>
                      </div>
                      <img src="uploads/profile/image.png" alt="avatar 1" style="width: 45px; height: 45px;" class="rounded-circle">
                    </div>
                    <div class="d-flex flex-row justify-content-end mb-4 pt-1">
                      <div>
                        <p class="small p-2 me-3 mb-1 text-white rounded-3 bg-primary">This is a message from a user to another user</p>
                        <p class="small me-3 mb-3 rounded-3 text-muted d-flex justify-content-end">
                        <div class="card-header bg-white d-flex justify-content-between align-items-center p-3">
                          <h5 class="mb-0">Chat</h5>
                          <button type="button" class="btn btn-outline-primary btn-sm" onclick="closeForm()">
                            <i class="fa fa-close"></i>
                          </button>
                        </div>
    Code (markup):

     
    Jeremy Benson, Jan 29, 2023 IP