-1

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";
    }
}
4
  • 1
    Don't use count as parameter. Replace getDetails(count) with getDetails() Commented Feb 21, 2014 at 12:43
  • @Satpal Oh silly me! Fixed it! Post it as an answer and ill accept it. So you get your points Commented Feb 21, 2014 at 12:48
  • Oh, i just saw it, mark he's answer as the right when he post it... Commented Feb 21, 2014 at 12:49
  • @DJZorrow, Glab I could help. Commented Feb 21, 2014 at 12:54

1 Answer 1

1

As you have defined parameter as Global. You don't need to pass it as a parameter. When you declares parameter with same name. It will hide global parameter.

Thus, don't use count as parameter. i.e replace getDetails(count) with getDetails()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.