Hi thanks for reading, got the following code, was working and not it is not. I want timers for each item, timer countdown being pulled from MySQL.

Thanks.

<script type="text/javascript">
<?php $selectTimers = mysql_query("SELECT * FROM auctions ORDER BY itemID DESC"); while($fetchTimers = mysql_fetch_array($selectTimers)) { $itemID = $fetchTimers['itemID']; $itemYears = $fetchTimers['itemYears']; $itemMonths = $fetchTimers['itemMonths']; $itemDays = $fetchTimers['itemDays']; $itemHours = $fetchTimers['itemHours']; $itemMinutes = $fetchTimers['itemMinutes']; $itemSeconds = $fetchTimers['itemSeconds']; ?>
itemTimeLeft<?=$itemID;?> = new Date(<?=$itemYears;?>,<?=$itemMonths - 1;?>,<?=$itemDays;?>,<?=$itemHours;?>,<?=$itemMinutes;?>,<?=$itemSeconds;?>);<?="\n";?>
<? } ?>

function GetCount(ddate,iid){
	dateNow = new Date();
	amount = ddate.getTime() - dateNow.getTime();
	delete dateNow;
	
	if(amount <= 0){
		ended = document.getElementById(iid);
		ended.innerHTML = "Auction Ended";
	}else{
		days=0;hours=0;mins=0;secs=0;out="";
		amount = Math.floor(amount/1000);
		days=Math.floor(amount/86400);//days
		amount=amount%86400;
		hours=Math.floor(amount/3600);//hours
		amount=amount%3600;
		mins=Math.floor(amount/60);//minutes
		amount=amount%60;
		secs=Math.floor(amount);//seconds
		if(days != 0){out += days +" "+((days==1)?"day":"days")+", ";}
		if(hours != 0){out += hours +" "+((hours==1)?"hour":"hours")+", <br />";}
		out += mins +" "+((mins==1)?"min":"mins")+", ";
		out += secs +" "+((secs==1)?"sec":"secs")+", ";
		out = out.substr(0,out.length-2);
		document.getElementById(iid).innerHTML=out;	
		setTimeout(function(){GetCount(ddate,iid)}, 1000);
	}
}

function GetInfo(itemID) {
	$('#bidAmount'+itemID).load('includes/auctions/getbid.php?itemID='+itemID);
	$('#bidUsername'+itemID).load('includes/auctions/getbidder.php?itemID='+itemID);
<?php $selectTimers = mysql_query("SELECT * FROM auctions ORDER BY itemID DESC"); while($fetchTimers = mysql_fetch_array($selectTimers)) { $itemID = $fetchTimers['itemID']; ?>
	setInterval(GetInfo("<?=$itemID;?>"), 1000);
<? } ?>
}

window.onload=function(){
<?php $selectTimers = mysql_query("SELECT * FROM auctions ORDER BY itemID DESC"); while($fetchTimers = mysql_fetch_array($selectTimers)) { $itemID = $fetchTimers['itemID']; ?>
	GetCount('itemTimeLeft<?=$itemID;?>', 'itemTimer<?=$itemID;?>');<?="\n";?>
	GetInfo("<?=$itemID;?>");<?="\n";?>
<? } ?>
};
</script>
Member Avatar for stbuchok

What is going wrong? Please explain, in detail, what you "want" to happen and what "is" happening.

BlueCharge,

Remember that vars in javascript functions are global unless localised with var .

var itemTimeLeft<?=$itemID;?> = new Date(...);
...
var amount = ddate - new Date();
...
var ended = document.getElementById(iid);
...
var days, hours, mins, secs, out="";
...
etc.

Airshow

Going further, you can loop just once in php to build a javascript array, then allow javascript/jquery to look after things.

I'm not sure this is 100% correct but you should get the idea:

window.onload=function(){
	var items = [];//javascript array
	var i = 0;

	function GetCount(){
		items.each(function(i, obj){
			var amount = obj.endTime - new Date();
			if(amount <= 0){
				obj.$ended.html("Auction Ended");
			}else{
				var days, hours, mins, secs, out="";
				amount = Math.floor(amount/1000);
				days=Math.floor(amount/86400);//days
				amount=amount%86400;
				hours=Math.floor(amount/3600);//hours
				amount=amount%3600;
				mins=Math.floor(amount/60);//minutes
				amount=amount%60;
				secs=Math.floor(amount);//seconds
				if(days != 0){out += days +" "+((days==1)?"day":"days")+", ";}
				if(hours != 0){out += hours +" "+((hours==1)?"hour":"hours")+", <br />";}
				out += mins +" "+((mins==1)?"min":"mins")+", ";
				out += secs +" "+((secs==1)?"sec":"secs")+", ";
				out = out.substr(0,out.length-2);
				obj.$ended.html(out);
			}
			setTimeout(GetCount, 1000);
		});
	}

	function GetInfo(i) {
		var obj = items[i];
		obj.$bidAmount.load('includes/auctions/getbid.php?itemID='+obj.iid);
		obj.$bidUsername.load('includes/auctions/getbidder.php?itemID='+obj.iid, function(){
			setTimeout(function(){GetInfo(i);}, 1000);
		});
	}

<?php
$selectTimers = mysql_query("SELECT * FROM auctions ORDER BY itemID DESC"); 
while($fetchTimers = mysql_fetch_array($selectTimers)) { 
	$itemID = $fetchTimers['itemID'];
	$itemYears = $fetchTimers['itemYears'];
	$itemMonths = $fetchTimers['itemMonths'];
	$itemDays = $fetchTimers['itemDays'];
	$itemHours = $fetchTimers['itemHours'];
	$itemMinutes = $fetchTimers['itemMinutes'];
	$itemSeconds = $fetchTimers['itemSeconds'];
?>
	var iid = <?=$itemID;?>;
	items[i] = {iid:iid, $bidAmount:$('#bidAmount'+iid), $bidUsername:$('#bidUsername'+iid), endTime:new Date(<?=$itemYears;?>,<?=$itemMonths - 1;?>,<?=$itemDays;?>,<?=$itemHours;?>,<?=$itemMinutes;?>,<?=$itemSeconds;?>};
	GetInfo(i);//kickstart GetInfo for item i
	i++;
<? } ?>
	GetCount();//kickstart GetCount for all items
};

Even better would be to modify GetInfo to call just one php script, getbiddcata.php, which returns all the necessary data for all relevant auctions in one hit. Individual calls will be inefficient due to browser overheads and probably IP packet utilisation.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.