I am beginner and I was trying to dynamically load stuff from database which was successful. Then my next goal was to implement infinite scroll with javascript, but I think the HTML code is being injected into wrong places or something, because when I use data-columns
and try to load more with scroll, the styling goes crazy. If I take that out, it loads fine, but the style of my page is all over the place.
Screenshot how is should look like
How it looks when I load 4 items at the start and then additional 4 when reaching bottom page
Index.php html in which the data is injected
<div id="fh5co-main">
<div class="container">
<div class="row">
<div id="fh5co-board" class="results" data-columns>
</div>
</div>
</div>
</div>
Javascript for infinite load in index.php
<script type="text/javascript">
var start = 0;
var limit = 4;
var reachedMax = false;
$(window).scroll(function () {
if($(window).scrollTop() == $(document).height() - $(window).height())
getData();
});
$(document).ready(function () {
getData();
});
function getData() {
if(reachedMax)
return;
$.ajax({
url: 'load_more.php',
method: 'POST',
dataType: 'text',
data: {
getData: 1,
start: start,
limit: limit
},
success: function (response) {
if (response == "reachedMax")
reachedMax = true;
else {
start += limit;
$(".results").append(response);
}
}
});
}
</script>
load_more.php
<?php include_once "includes/functions.php"?>
<?php
if(isset($_POST['getData'])) {
$start = $_POST['start'];
$limit = $_POST['limit'];
$query = "SELECT title, image, text FROM posts LIMIT $start, $limit";
$result = mysqli_query(DatabaseConnect(), $query);
if (mysqli_num_rows($result) > 0) {
$response = "";
while ($data = mysqli_fetch_array($result)) {
$response .= '
<div class="item">
<div class="animate-box">
<a href="images/' . $data['image'] . '" class="image-popup fh5co-board-img" title="' . $data['title'] . '"><img src="images/' . $data['image'] . '" alt="image"></a>
</div>
<h3>' . $data['title'] . '</h3>
<div class="fh5co-desc">' . $data['text'] . '</div>
</div>';
}
exit($response);
} else
exit('reachedMax');
}
</div>