drwpbrv668670 2014-11-20 09:33
浏览 30
已采纳

通过数据库行查询结果乘以Jquery Time Counter

I have this javascript for displaying time counter :

$(function(){

        var note = $('.note'),
        ts = new Date(<?php echo $tgl_close1; ?>),
        newYear = true;

    if((new Date()) > ts){
        newYear = false;
    }

    $('.countdown').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 />";
            note.html(message);
        }
    });

});

i get the $tgl_close value from database. It working fine if i just call 1 row from my table, but i need to make a multiply time counter by the result of database query.

this is how my table looked :

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

i tried the stupid way by inserting the javascript into the foreach loop but it didn't work.. this is how i tried :

<?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"></div>
    <p class="note"></p>    
    </td></tr></table>

    <!-- JavaScript includes -->
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="assets/countdown/jquery.countdown.js"></script>
    <script type="text/javascript">
    $(function(){

        var note = $('.note'),
        ts = new Date(<?php echo $tgl_close; ?>),
        newYear = true;

    if((new Date()) > ts){
        newYear = false;
    }

    $('.countdown').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 />";
            note.html(message);
        }
    });

});

</script>
<?php
}
}
mysql_close($conn);
?>

can someone show me how to do it?

  • 写回答

1条回答 默认 最新

  • dongzhankou2090 2014-11-20 10:11
    关注

    I don't have a place to test this and I'm not sure what you want your final html output to look like, but I think this should get you close:

    <!-- JavaScript includes -->
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="assets/countdown/jquery.countdown.js"></script>
    <?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)) {
    ?>
    <br>
    <table border="0"><tr><td>
        <div class="countdown" data-ts="<?php echo date("r", strtotime($row['tgl_close1'])); ?>"></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($this.data('ts')),
                    newYear = new Date() <= ts;
                $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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致