如何使用此方法遍历mysql数据库表

时间:2015-11-21 12:36:52

标签: javascript php mysql

如何使用此方法遍历mysql数据库表并根据存储在其中一个字段中的日期/时间为表中的每条记录显示一个计时器?

$sql = "SELECT * FROM post";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$Row = (mysqli_fetch_assoc($result));
$th = $Row['endtime'];    
}



var today = new Date();
var DD = today.getDate();
var MM = today.getMonth()+1; //January is 0!
var YYYY = today.getFullYear();
//let get the Difference in Sec btw the two dates
var _DateFromDBProgEndDate = '<?php echo $th; ?>';
var ProgEndTime = new Date(_DateFromDBProgEndDate);
var TodayTime = new Date();

var differenceTravel = ProgEndTime.getTime()- TodayTime.getTime() ;
var seconds = Math.floor((differenceTravel) / (1000));
////////////////////////////////
var SecDiffFromToday = seconds;
var seconds = SecDiffFromToday;
function timer() {
    var days        = Math.floor(seconds/24/60/60);
    var hoursLeft   = Math.floor((seconds) - (days*86400));
    var hours       = Math.floor(hoursLeft/3600);
    var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
    var minutes     = Math.floor(minutesLeft/60);
    var remainingSeconds = seconds % 60;
    if (remainingSeconds < 10) {
        remainingSeconds = "0" + remainingSeconds; 
    }
    document.getElementById('countdown').innerHTML = days + ":" + hours + ":" + minutes + ":" + remainingSeconds;
    if (seconds == 0) {
        clearInterval(countdownTimer);
        document.getElementById('countdown').innerHTML = "Completed";
    } else {
        seconds--;
    }
}
var countdownTimer = setInterval('timer()', 1000);

这是我的javascript

1 个答案:

答案 0 :(得分:1)

这是一个可能的解决方案。它从数组中收集数据库中的日期:

<强> PHP

<?php
$sql = "SELECT * FROM post";
$result = mysqli_query($conn, $sql);
$th = [];
while ($row = mysqli_fetch_assoc($result)) {
    $th[] = "'" . date('U', $row['endtime']) . "'";
}
?>

然后在生成javascript的部分中,使用此$th变量填充javascript数组:

var _DateFromDBProgEndDate = [<?=implode(",", $th)?>];

然后javascript部分继续如下:

<强>的Javascript

function pad(n) {
   // utility function to pad a number to 2 digits:
   return ('0' + n).substr(-2); 
}

function timer() {
    var todaySeconds = Math.floor(new Date().getTime() / 1000);
    // collect the html first in an array
    var html = [];
    // keep track whether there are still counters not yet zero:
    var allDone = true;
    // go through the list of dates:
    endDatesInSeconds.forEach(function (endDateSeconds) {
        var totalSeconds = endDateSeconds - todaySeconds;
        var days        = Math.floor(totalSeconds/24/60/60);
        var seconds     = Math.floor(totalSeconds - days*86400);
        var hours       = Math.floor(seconds/3600);
        seconds         = Math.floor(seconds - hours*3600);
        var minutes     = Math.floor(seconds/60);
        seconds         = seconds % 60;
        // add a part of html
        html.push(
            totalSeconds <= 0 
               ? 'Completed'
               : days + ":" + pad(hours) + ":" + pad(minutes) + ":" + pad(seconds));
        // when at least one counter is not complete, we are not done:
        allDone = allDone && totalSeconds <= 0;
    });
    // now put the html in the document:
    document.getElementById('countdown').innerHTML = html.join('<br/>');
    if (allDone) {
        clearInterval(countdownTimer);
    }
}
var countdownTimer = setInterval('timer()', 250);

这是一个fiddle,它没有php部分,但使用了四个虚拟时间来处理。

关于原始代码的一些注意事项:

  • 测试seconds == 0是危险的,因为无法保证定时器每秒都会准确记录,并且无法保证数据库返回的时间不是过去的。在这两种情况下,你都可以获得负秒,而你的计时器实际上永远不会停止。
  • 您将seconds变量减少为1,但由于无法保证计时器每秒都会准确记录,因此您将远离实际时差。因此,最好再次获得当前时间,并再次计算差异。我的答案是我让计时器更频繁地打勾,以获得更准确的结果。
  • 你有一些代码用秒前零点填充秒部分,但是你也想要这几分钟甚至几小时。