我正在使用这个时间计数器,需要根据数据库中的行总数多次显示它。
我有这个javascript用于显示时间计数器:
$(function(){
$('.countdown').each(function() {
var $this = $(this),
ts = new Date($('.note').data('ts')),
newYear = true;
if((new Date()) > ts){
newYear = false;
}
$this.countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds){
var message = "";
message += days + " hari" + ( days==1 ? '':'' ) + ", ";
message += hours + " jam" + ( hours==1 ? '':'' ) + ", ";
message += minutes + " menit" + ( minutes==1 ? '':'' ) + ", ";
message += seconds + " detik" + ( seconds==1 ? '':'' ) + " <br />";
$this.next().html(message);
}
});
});
});
这就是我的表格:
id | tgl_close1 | idrek
1 | 2014-11-25 08:00:00 | 1
2 | 2014-11-26 10:00:00 | 1
3 | 2014-11-26 12:10:00 | 1
我将倒计时乘以foreach,但所有计数器总是根据第一行tgl_close1
计算时间而不是获取下一行值。
这是我的完整代码:
<?php
$fetch = mysql_query("select tgl_close1
from tba
where idrek = 1");
/* Retrieve and store in array the results of the query.*/
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$tgl_close1[] = date("Y, n-1, j, G, i, s", strtotime($row['tgl_close1']));
}
foreach ($tgl_close1 as $tglclose){
?>
<br>
<table border="0"><tr><td>
<div class="countdown" data-ts="<?php echo $tglclose"></div>
<p class="note"></p>
</td></tr></table>
<?php
}
}
mysql_close($conn);
?>
<script type="text/javascript">
$(function(){
$('.countdown').each(function() {
var $this = $(this),
ts = new Date($('.note').data('ts')),
newYear = true;
if((new Date()) > ts){
newYear = false;
}
$this.countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds){
var message = "";
message += days + " hari" + ( days==1 ? '':'' ) + ", ";
message += hours + " jam" + ( hours==1 ? '':'' ) + ", ";
message += minutes + " menit" + ( minutes==1 ? '':'' ) + ", ";
message += seconds + " detik" + ( seconds==1 ? '':'' ) + " <br />";
$this.next().html(message);
}
});
});
});
</script>
有人可以告诉我如何在data-ts
属性中设置不同的值吗?
答案 0 :(得分:1)
您正在阅读ts = new Date($('.note').data('ts'))
,这只会给您第一个注释。
您需要阅读note
div旁边的countdown
,将其更改为以下代码
ts = new Date($(this).next('.note').data('ts'))