I´m sending a formula which is retrieving data through PHP from a MySQL DB. I want to collect the data step by step, by scrolling, a kind of "infinite scrolling" implementation written in Javascript. It´s working fine, except for the fact that I can not find the way to increment the value of the variable that specify which row to start from retrieving data, every time that I call the PHP function from inside a script in my HTML document.
Below is the index.php code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script-->
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/copy_code.js"></script>
<script src="js/scroll.js"></script>
<script src="js/preloader.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
<?php
include("dbconnect.php");
include("search.php");
?>
</head>
<body>
<!-- Preloader -->
<div class="preloader"></div>
<!-- scroll to top buttom -->
<a href="#" id="scroll"><span></span></a>
<article>
<div class="entry_content">
<FORM method=post action='<?php echo htmlentities($_SERVER['PHP_SELF']); ?>'>
<!-- My FORM -->
</FORM>
<div id="load_data"> </div>
<div id="load_data_message"></div>
</div>
</article>
</body>
</html>
<script>
$(document).ready(function(){
var limit = 10;
var start = 0;
var action = 'inactive';
console.log("Limite + Start:")
console.log(limit);
console.log(start);
function load_data(limit, start)
{
console.log("Limite & Start");
console.log(limit);
console.log(start);
var data="<?php echo search(limit, start); ?>";
$('#load_data').append(data);
if(data == '') /* No data: active, "no data found" */
{
$('#load_data_message').html("<button type='button' class='btn btn-info'>No Data Found</button>");
action = 'active';
}
else /* data: active, "Please wait..." */
{
$('#load_data_message').html("<button type='button' class='btn btn-warning'>Please Wait....</button>");
action = "inactive";
}
}
if(action == 'inactive')
{
console.log("Inactiva");
action = 'active';
load_data(limit, start);
}
$(window).scroll(function(){
if($(window).scrollTop() + $(window).height() > $("#load_data").height() && action == 'inactive')
{
action = 'active';
console.log("Activaaaaaa. Start: ");
start = start + limit;
console.log(start);
setTimeout(function(){
load_data(limit, start);
}, 1000);
}
});
});
</script>
The search(limit, start) function is inside a search.php file and it is retrieving the values in this way:
$result = $mysqli->query("SELECT * FROM MyTable WHERE $conditions LIMIT $start, $limit");
while ($row = mysqli_fetch_object($result)) {
echo [...]
}
I want to increment this $start variable every time I call the load_data(limit, start) function from index.php.
I tried creating cookies (didn´t work because I can not get the new cookies values before refreshing), creating a static php variable inside the search function and incrementing its value (didn´t work, I do not know why), passing a variable through the form and incrementing its value,... but nothing worked.