如何使这个JS倒计时代码依赖于DB值?

时间:2017-06-05 19:02:17

标签: javascript php

我有JS代码,这是一个倒计时。我希望它从数据库中获取一个值并从该值开始倒计时。因此,如果数据库中的值为8,则应从8小时开始倒计时。我怎么能做到这一点?

代码:

In [48]: mat = np.random.rand(100,1000)

In [50]: mat[:,np.r_[0:13,19:23,23:31,39:59]].shape
Out[50]: (100, 45)

In [51]: 13+4+8+20
Out[51]: 45

3 个答案:

答案 0 :(得分:0)

您也可以通过

在javascript中调用php
var time_limit = <?php echo $db_value; ?>

答案 1 :(得分:0)

你可以很容易地做到这一点。它工作正常。从数据库中检索时间然后它转换h:m:s然后将值传递给javascript并倒计数值

require_once 'db_connect.php';

$sql = "SELECT * FROM your_table_name"; 
$result = $con->query($sql);

if( $result->num_rows > 0 ){
    while($row=mysqli_fetch_assoc($result)) {
        $date_time = $row['date_time']; // column name date_time 10 hours
    }
}

$timestamp = time();
$diff = $date_time*60*60); //<-Time of countdown in seconds.  ie. 3600 = 1 hr. or 86400 = 1 day.

//MODIFICATION BELOW THIS LINE IS NOT REQUIRED.
$hld_diff = $diff;
if(isset($_SESSION['ts'])) {
    $slice = ($timestamp - $_SESSION['ts']);    
    $diff = $diff - $slice;
}

if(!isset($_SESSION['ts']) || $diff > $hld_diff || $diff < 0) {
    $diff = $hld_diff;
    $_SESSION['ts'] = $timestamp;
}

//Below is demonstration of output.  Seconds could be passed to Javascript.
$diff; //$diff holds seconds less than 3600 (1 hour);

$hours = floor($diff / 3600) . ' : ';
$diff = $diff % 3600;
$minutes = floor($diff / 60) . ' : ';
$diff = $diff % 60;
$seconds = $diff;
?>

//This is java script

<div id="strclock">Clock Here!</div>
<script type="text/javascript">
var hour = <?php echo floor($hours); ?>;
var min = <?php echo floor($minutes); ?>;
var sec = <?php echo floor($seconds); ?>

function countdown() {
    if(sec <= 0 && min > 0) {
        sec = 59;
        min -= 1;
    }
    else if(min <= 0 && sec <= 0) {
        min = 0;
        sec = 0;
    }
    else {
        sec -= 1;
    }

    if(min <= 0 && hour > 0) {
        min = 59;
        hour -= 1;
    }

    var pat = /^[0-9]{1}$/;
    sec = (pat.test(sec) == true) ? '0'+sec : sec;
    min = (pat.test(min) == true) ? '0'+min : min;
    hour = (pat.test(hour) == true) ? '0'+hour : hour;

    document.getElementById('strclock').innerHTML = hour+":"+min+":"+sec;

    setTimeout("countdown()",1000);

}
countdown();


</script>

答案 2 :(得分:0)

您的代码似乎没有像您期望的那样返回列值。显然,这是您当前代码的解释方式(详见代码注释)

if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}
else{
  $sql = "SELECT roundstime FROM vf_Discussion";
  $roundstime = $con->query($sql); //here $roundstime is the result object returned by the query

  var_dump($roundstime); //dumps the result object fetched in the previous statement

  echo 'successful';
}

因为元素

<input type="hidden" id="set-time" value="<?php echo htmlspecialchars($roundstime); ?>"/>

回显的值不是列的值,而是以下错误

  

捕获致命错误:类mysqli_result的对象无法转换为字符串

使用以下代码替换if...else块可能会达到您目的的50%:

if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}
else{
    $sql = "SELECT roundstime FROM vf_Discussion";
    $roundstime = $con->query($sql);
    $row=$roundstime->fetch_assoc(); // this statement fetches the associative array from the object now $row becomes an associative array

    echo 'successful';
}

对于其他50%,这只是html元素中的一个小编辑

<input type="hidden" id="set-time" value="<?php echo $row[`roundstime`] ?>"/>

有关详细信息,请参阅以下网址。 Selecting data From mysql using PHP(object oriented)