0

I am displaying data from database by Ajax and I need to add countdown timer with times from database. Is it possible to do it as I am struggling with it for about 24 hours. You can look at main code here http://www.egrupper.pl and as you will see the countdown timers are not working. The AJAX code:

function showDeals(sid,limit,cat)
{
sid=typeof sid !== 'undefined' ? a : 1;
limit = typeof limit !== 'undefined' ? limit : 0.7;
if(sid=="" || limit==""){
    document.getElementById("con").innerHTML="";
    return;
}
if(window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
}else{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById("con").innerHTML=xmlhttp.responseText;
$('#con').masonry({
      itemSelector: '.clsDeal_Blk_Cont'
    });

    }
}
xmlhttp.open("GET","getdeals.php?sid="+sid+"&limit="+limit+"&cat="+cat,true);
xmlhttp.send();
}

and in getdeals.php I have countdown timer code which looks like this:

$(document).ready(function(){
            $("#countdown_<?php echo $id; ?>").countdown({
                date: "date_from_database",
                format: "on"
            },

            function() {
                // callback function
            });
        });

thanks @user2008945 for help which was useful for me, but anyway I need more than one countdown timers on page and I though that something like this will do but unfortunatelly not:

success:function(rows){

for (var i in rows){
var row=rows[i];
var id=row[0];
var end_date=row[1];
$("#countdown_"+id).countdown({
            date: end_date,
            format: "on"
        },

        function() {
            // callback function
        });
}
}

and

$data=array();
while($row=mysql_fetch_row($result))
{
    $data[]=$row;
}
die (json_encode($data));
6
  • 2
    why don't you use jquery ajax? Commented Feb 7, 2013 at 20:25
  • as I think it is the best way to get data from database without reloading website Commented Feb 7, 2013 at 20:29
  • 1
    please review this link: api.jquery.com/jQuery.ajax Commented Feb 7, 2013 at 20:30
  • 1
    jQuery ajax does not refresh the page either,its just a cleaner form of using ajax with cross browser support as well, the same thing you are doing now. Commented Feb 7, 2013 at 20:31
  • Other way is to use sockets. read more about socket.io Commented Feb 7, 2013 at 20:32

1 Answer 1

1

Unless you are familiar with jquery ajax, this code would not be much of help though, but if you decide to use jquery ajax, then you can use this code:

$(document).ready(function(){

$.ajax({
url:"getdeals.php?sid="+sid+"&limit="+limit+"&cat="+cat,
type:"GET",
dataType:'json',
success:function(data){

//data contains value like {countDownId:'someid',countDownDate:'somedate'},
//its in json format,
// in your getdeals.php write some thing like this 
// die (json_encode(array('countDownId'=>'someid','countDownDate'=>'somedate')));

$("#countdown_"+data.countDownId).countdown({
                date: data.countDownDate,
                format: "on"
            },

            function() {
                // callback function
            });

}
});

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

3 Comments

thanks @user2008945 for help which was useful for me, but anyway I need more than one countdown timers on page and I though that if I will put array with results from database and do 'for loop' that it will help, but unfortunatelly not. I have edited my question to show what I have done.
if rows looks something like this : rows={0:{0:'someid',1:'somedate'},1:{0:'someid1',1:'somedate2'}}, it should work. Try using control.log to see what is the structure of 'rows'.
the data is alright as one timer is working, the problem is that just the last one is counting down, but the others has got some time, but they are not couting down, just showing all the time the same time

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.