如何追加数据并加载

时间:2019-06-21 17:38:13

标签: php append load polling

所以我有这个轮询脚本,并且应该从php文件加载数据并加载它,但是它不起作用。 我在加载方面$("#allMessages").load("../Admin/UsersMessages/newmessage.php #messages2");遇到了这个问题,所有消息都在

这样的div中
echo '

 shadow" style="background-color:'.$chatColors.'">
        <span class="usersname" name="receiver"><a href="Account?='.$Username.'">'.$Username.'</a></span><span onclick="CloseMessage()" class="closeMessage"> X </span>
<hr style="margin-top:24px">
<div id="allMessages"class="allMessages">
';

// $sql2 = "SELECT * FROM messages WHERE Sender = '$Myself' ORDER BY `Id` ASC";
// $result2 = $conn->query($sql2);

$sql3 = "SELECT * FROM messages 
        WHERE (Sender = '$Myself' AND Receiver = '$Username')
           OR (Receiver = '$Myself' AND Sender = '$Username') ORDER BY `MessageTime` ASC";


$result3 = $conn->query($sql3);

$x = 0;

$sql4 = "SELECT * FROM messages WHERE Sender = '$Myself' AND groupName = ''";
$result4 = $conn->query($sql4);

$sql5 = "SELECT * FROM messages WHERE Receiver = '$Myself' AND groupName = ''";
$result5 = $conn->query($sql5);

$totalSender = mysqli_num_rows($result4);
$totalReceiver = mysqli_num_rows($result5);

// might have to add both totals for while loop
// to get total messages
$totalmessages = $totalSender + $totalReceiver;
// For receiving
if(mysqli_num_rows($result3) > 0){
while ($x < $totalmessages) {



    // output data of each row

     if ($result3->num_rows > 0) {
         if ($row = $result3->fetch_assoc()) {
             $Sender = $row['Sender'];
             $Message = $row['Message'];
             $Receiver = $row['Receiver'];
             $TimeSent = $row['MessageTime'];
         }
     }
     global $Sender;
     global $Message;
     global $Receiver;
     global $TimeSent;


date_default_timezone_set('US/Eastern');
      $sql4 = "SELECT * FROM usersettings WHERE Username = '$Sender'";
          $result4 = $conn->query($sql4);
          if ($result4->num_rows > 0) {
      // output data of each row
            if($row = $result4->fetch_assoc()) {
              $ProfilePicture = $row['ProfilePicture'];
            }
          } 


      $class = $Sender == $Myself ? 'messageDisplay' : 'messageDisplay2';
       $class2 = $Sender != $Myself ? 'chatImage' : 'chatImage2';


     // sending a message

        echo '<div id="messages2" class="'.$class.'">';
             echo '<a href=../Site/Account?='.$Sender.'><object class="'.$class2.'" data="data:image/jpg;base64,'.base64_encode($ProfilePicture).'"/ type="image/png"><img style="width:30px;height:30px;position:relative;margin: auto;border-radius:100px" src="../Assets/Images/NoImage.png" alt="example"></object></img></a>';
             echo ''.htmlspecialchars($Message).'<span style="float: right;margin-top:2px"><font size=1>'.time_elapsed_string($TimeSent).'</span></font></div>';



$x++;
}
}   


// Typing part of message
echo '
</div><div id="newMessage"></div>
      <input type="text" id="messagetosend" autocomplete="off" name="message" class="messagetype" placeholder="Type a message..."><span onclick="send3();poll2()" class="messageSend">↩</span>

</div>


';

应该加载的数据

    <?php session_start();
$Myself = $_SESSION['username'];
$Username = $_COOKIE['UsersMessages'];
include "../../Site/db.php";



    function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}



$sql3 = "SELECT * FROM messages 
        WHERE (Sender = '$Myself' AND Receiver = '$Username')
           OR (Receiver = '$Myself' AND Sender = '$Username') ORDER BY `MessageTime` DESC LIMIT 1";


$result3 = $conn->query($sql3);

$x = 0;

$sql4 = "SELECT * FROM messages WHERE Sender = '$Myself' AND groupName = ''";
$result4 = $conn->query($sql4);

$sql5 = "SELECT * FROM messages WHERE Receiver = '$Myself' AND groupName = ''";
$result5 = $conn->query($sql5);

$totalSender = mysqli_num_rows($result4);
$totalReceiver = mysqli_num_rows($result5);

// might have to add both totals for while loop
// to get total messages
$totalmessages = 1;
// For receiving
if(mysqli_num_rows($result3) > 0){
while ($x < $totalmessages) {



    // output data of each row

     if ($result3->num_rows > 0) {
         if ($row = $result3->fetch_assoc()) {
             $Sender = $row['Sender'];
             $Message = $row['Message'];
             $Receiver = $row['Receiver'];
             $TimeSent = $row['MessageTime'];
         }
     }
     global $Sender;
     global $Message;
     global $Receiver;
     global $TimeSent;


date_default_timezone_set('US/Eastern');
      $sql4 = "SELECT * FROM usersettings WHERE Username = '$Sender'";
          $result4 = $conn->query($sql4);
          if ($result4->num_rows > 0) {
      // output data of each row
            if($row = $result4->fetch_assoc()) {
              $ProfilePicture = $row['ProfilePicture'];
            }
          } 


      $class = $Sender == $Myself ? 'messageDisplay' : 'messageDisplay2';
       $class2 = $Sender != $Myself ? 'chatImage' : 'chatImage2';


     // sending a message

        echo '<div id="messages2" class="'.$class.'">';
             echo '<a href=../Site/Account?='.$Sender.'><object class="'.$class2.'" data="data:image/jpg;base64,'.base64_encode($ProfilePicture).'"/ type="image/png"><img style="width:30px;height:30px;position:relative;margin: auto;border-radius:100px" src="../Assets/Images/NoImage.png" alt="example"></object></img></a>';
             echo ''.htmlspecialchars($Message).'<span style="float: right;margin-top:2px"><font size=1>'.time_elapsed_string($TimeSent).'</span></font></div>';



$x++;
}
} 



?>

为什么这不会加载到allmessages div中?如果需要更多信息,我可以编辑帖子。谢谢!

0 个答案:

没有答案