Ciao a tutti.
Sto realizzando un sito che visualizza vari countdown in base ad una data letta dal DB. Ho creato questa porzione di codice:
<div id="main<?php echo $content->id; ?>">
<div class="prova">
<p>
<?php
$dateDB = 5554343434;
$arrayDateDB = explode("-", $dateDB);
$month=$arrayDateDB[1];
$dayArray = explode(" ", $arrayDateDB[2]);
$day=$dayArray[0];
$year=$arrayDateDB[0];
$timeDB = explode(":", $dayArray[1]);
$hours = $timeDB[0];
$minutes = $timeDB[1];
$seconds = $timeDB[2];
$target = mktime($hours, $minutes, $seconds, $month, $day, $year);
$today = time ();
$difference =($target-$today);
$seconds = $difference;
$idDb = "cntdwn".$content->id;
?>
<html>
<script language="JavaScript" type="text/JavaScript">
var intCountDown = <?php echo $seconds; ?>;
//var idDiv = 'cntdwn_'+<?php echo $idDb; ?>;
var idDiv = <?php echo $idDb; ?>;
function countDown() {
if (intCountDown <= 0) {
<?php echo $idDb; ?>.innerHTML = '0';
return;
} else {
var difference = intCountDown--;
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(difference/oneDay)
var hourfield=Math.floor((difference-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((difference-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((difference-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
dayfield = dayfield+'';
if (dayfield.length<2) {
dayfield = "0" + dayfield;
}
hourfield = hourfield+'';
if (hourfield.length<2) {
hourfield = '0' + hourfield;
}
minutefield = minutefield+'';
if (minutefield.length<2) {
minutefield = '0' + minutefield;
}
secondfield = secondfield+'';
if (secondfield.length<2) {
secondfield = '0' + secondfield;
}
<?php echo $idDb; ?>.innerHTML = dayfield + ' Giorni ' + hourfield + ':' + minutefield + ':' + secondfield;
setTimeout("countDown()", 999);
}
}
</script>
<body onload="javascript:countDown();">
<div id="<?php echo $idDb; ?>"></div>
</body>
</html>
</p>
</div>
</div>
Se inserisco solo una riga nel DB, il countdown funziona in modo corretto, ma se ci sono due righe da far visualizzare con due countdown differenti, si visualizza solo l'ultimo. Sapreste indicarmi cosa sbaglio?
Ciao e grazie