I am creating a social media style website and was trying to emulate multiple users on at the same time by opening multiple tabs. When I opened the task manager I noticed my apache web server was running about 20% cpu after debugging and moving around scripts I found the cause was here
var cnodelist1 = jQuery_3_3_1("#CheckInContainer").length;
function CheckTT()
{
var cnodelist2 = jQuery_3_3_1("#CheckInContainer").length;
if(cnodelist2 > cnodelist1)
{
var audio = jQuery_3_3_1("#mysoundclip")[0];
audio.play();
audio.volume = 0.1;
cnodelist1 = cnodelist2;
}
//Problem is loading alot of content
jQuery_3_3_1("#CheckInContainer").load(location.href+" #CheckInContainer>*","");
}
setInterval(CheckTT, 5000);
which loads the the updated post container
<div id = "CheckInContainer">
<?php
$sql2 = "SELECT author,post,date FROM mypage_posts ORDER BY date DESC";
$con2 = DataBaseConnect();
$result2 = mysqli_query($con2, $sql2);
$message = '';
$date = '';
$allmsgs = "";
if(!$result2)
{
$errors[] = "* Error: " . $sql2 . "<br>" . mysqli_error($con2)."<br>";
}
else
{
while($row2 = mysqli_fetch_assoc($result2))
{
$message = isset($row2['post']) ? $row2['post'] : '';
$date = isset($row2['date']) ? $row2['date'] : '';
$auth = isset($row2['author']) ? $row2['author'] : 'Guest';
$sql3 = "SELECT user_avatar FROM accounts WHERE user_displayname = '$auth'";
$con3 = DataBaseConnect();
$result3 = mysqli_query($con3, $sql3);
$av = 'i/guest.png';
while($row3 = mysqli_fetch_assoc($result3))
{
$av = isset($row3['user_avatar']) ? $row3['user_avatar'] : 'i/guest.png';
}
if(!empty($message))
{
$message = htmlspecialchars($message);
$message = wordwrap(nl2br($message),208,"\n",true);
$message = autolink($message, array("target"=>"_blank","rel"=>"nofollow"));
$dont_send = false;
$type = "<b>Global</b>";
$en = '';
if($message[0] == "@")
{
$n = isset($_SESSION['user_name']) ? $_SESSION['user_name'] : '';
$l = strlen($n);
if(empty($n))
{
$dont_send = true;
}
else
{
for($i = 1;$i<$l;++$i)
{
if($message[$i] != $n[$i-1])
{
echo '$message[$i]';
$dont_send = true;
break;
}
}
}
if(!$dont_send)
{
$message = substr($message, $l+2);
$type = "<b>Private</b>";
}
}
if(!$dont_send && $_SESSION['entered'])
{
$allmsgs.= '<pre id ="ptext"style = "text-align:left;position:relative;"><img src = "'.$av.'" style = "width:32px; height:32px; padding:16px;padding-left:0px;padding-bottom:0px;" alt="Avatar" /><span style = "position: absolute;top: 16px;left: 42px;"><b>'.$auth.'</b><br><i>'.time_elapsed_string($date,false).' '.$type.'</i></span><br><br><br>'.TextToEmotes(wordwrap(nl2br($message),69,"\n",true)).'</pre>
<div class="blank_bar" style = "width:100%;border: 1px solid #586167;"><a href="" style = "padding-right: 30px;">Rate</a><a href="">Reply</a></div><div name = "seperator" id = "seperator"></div>';
}
}
}
if(!empty($allmsgs))
{
echo $allmsgs;
}
}
?>
</div>
The problem was I had well over 1000 posts in the database and of course loading that much content is ridiculous, so my question is how can I manage loading the needed content? I am thinking i would have to track verticle scroll location and load the content based of that then change my sql query to use from, where, between scrollposition , scrollposition + maxload, orderby,desc.
This is where I get lost I am not to sure how to piece all this together or if this is even the logical way to go about this.
You have a good idea about the scrollposition. That is what almost any social website uses. Now in order for you to make something like that you need this
$(window).scroll(function(){
if ($(window).scrollTop() == $(document).height()-$(window).height()){
alert("We're at the bottom of the page!!");
// Ajax request right here
}
});
Everytime you load more results, you need to count it as a page, and in the PHP script you make it work just like pagination
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.