I have some trouble with my JavaScript. This script uses Ajax GET to retrieve content from a PHP page. And it gets the content depending on a variable called count.
This code works until it gets to the last item in my database. When it gets no_data from the PHP page: It should reset the counter variable and start over from the top, and continue in a infinite loop. But when i try to reset the variable with count = 0;: Nothing happens. Why is that?
Here is my JS code:
var count = 0;
var timer;
$(window).load(function() {
load_new_entry();
// timer = window.setInterval(load_new_entry, 7500);
// window.clearTimeout(timer);
});
function load_new_entry() {
fade_out();
}
function fade_out() {
$(".container").fadeOut(1500, load_data);
window.clearTimeout(timer);
}
function fade_in() {
$(".container").fadeIn(1500);
timer = window.setInterval(load_new_entry, 7500);
}
function load_data() {
getDetails(count);
count++;
}
function getDetails(count) {
$.get("data.php?count="+count,function(data,status){
if (data != 'no_data') {
var jsonReturnData = $.parseJSON(data);
$(".container").html(jsonReturnData.content);
fade_in();
//console.log("Load details: " + count);
} else {
count = 1;
fade_in();
//console.log("Counter Reset: " + count);
}
});
}
And here is my PHP:
<?php
mysql_connect("127.0.0.1", "root", "");
mysql_select_db("infoscreen");
function grab_data($count) {
$get_data = mysql_query("SELECT * FROM display ORDER BY `id` ASC LIMIT ".$count.",1");
while ($data = mysql_fetch_assoc($get_data)) {
$id = $data['id'];
$content = $data['content'];
$return_data = array('id' => $id, 'content' => $content);
//echo $return_data['content'];
echo json_encode($return_data);
}
}
if (isset($_GET['count'])) {
$count = $_GET['count'];
$utf_8 = mysql_query("SET CHARACTER SET utf8");
$data_exists = mysql_query("SELECT * FROM display ORDER BY id ASC");
if (mysql_num_rows($data_exists) > $count) {
// Next record is available
grab_data($count);
} else {
// Next record is not available
echo "no_data";
}
}
countas parameter. ReplacegetDetails(count)withgetDetails()