【问题标题】:Call javascript everytime the while loop runs to give a unique time from database每次 while 循环运行时调用 javascript 以从数据库中提供唯一的时间
【发布时间】:2013-09-10 11:04:10
【问题描述】:

抱歉再次打扰,我有另一个关于 javascript 的问题

这是 mysql 数据库表中时间计数器的代码:

<script type='text/javascript'>        
        function cronometru(timp_ramas) {
            Ore = Math.floor(timp_ramas / 3600);
            minute = Math.floor((timp_ramas - Ore * 3600) / 60);
            secunde = timp_ramas - minute * 60 - Ore * 3600;
            if (Ore < 10){ Ore = "0"+ Ore; }
            if (minute < 10){ minute = "0" + minute; }
            if (secunde < 10){ secunde = "0" + secunde; }
            if (timp_ramas > 0) {

                timp_ramas--;
                document.getElementById("timp").innerHTML = Ore + ':' + minute + ':' + secunde;
                //document.getElementById("cumpara").innerHTML ="<br><a href='piata.php?cumpara=<?php echo $id_functie; ?>'>Cumpara</a>";

                setTimeout("cronometru("+timp_ramas+")", 1000);

            } else {
                document.getElementById("timp").innerHTML = "[Licitatia s-a terminat]";
            //  document.getElementById("cumpara").innerHTML = "[Nu mai poti cumpara]";

            }
        }
</script>

然后在php中我把这个脚本在while函数中调用成表格形式

while($informatie = mysql_fetch_array($sql))
{   
   $timp_ramas = $informatie['data_limita'] - time();

   //........................................

   echo " <td width='40%' align='justify'>
        Pret : ".$informatie['obiect_pret']." 
        <br />
        Timp ramas : <span id='timp'> "; 

   echo " <script type='text/javascript'> cronometru(".$timp_ramas.") </script></span> ";
   if ($informatie['obiect_cantitate'] > 1) 
       echo "<br>Cantitate disponibila: ".$informatie['obiect_cantitate']." ";
   if ($informatie['data_limita'] > 1) 
       echo "<br><a href='piata.php?cumpara=".$informatie['id']."'>Cumpara</a>";

   echo "</td>";
}

我不明白为什么如果我有 3 行,每行都有来自数据库的不同信息,那么只有首先访问 javascript。 其他行完美无缺,它们从数据库中检索所有信息,但这一行并不希望显示每一行的计时器(倒计时)。为什么?

【问题讨论】:

    标签: php javascript mysql sql


    【解决方案1】:

    这是我为我的拍卖网站编写的一些代码。它对列出的每个项目都有实时倒计时。我猜你正在做同样的事情。查看代码并随意使用它。无论如何,它应该能让你走上正确的道路。

    function countdown(x,t,b,i) {
    var days = Math.floor(t / 60 / 60 / 24);
    var hours = Math.floor([t - (days * 86400)] / 60 / 60 );
    var minutes = Math.floor([t - (days * 86400) - (hours * 3600)] / 60 );
    var seconds = [t - (days * 86400) - (hours * 3600) - (minutes * 60)];
    if ( seconds == 1 ){
    secondslabel = " Second"}
    else {
    secondslabel = " Seconds"}
    if ( minutes == 1 ){
    minuteslabel = " Minute "}
    else {
    minuteslabel = " Minutes "}
    if ( hours == 1 ){
    hourslabel = " Hour "}
    else {
    hourslabel = " Hours "}
    if ( days == 1 ){
    dayslabel = " Day "}
    else {
    dayslabel = " Days "}
    
    if ( days == 0 && hours == 0 && minutes == 0){
    document.getElementById(x).value=seconds + secondslabel;
    document.getElementById(x).style.backgroundColor = '#E19B9B';
    }
    else if ( days == 0 && hours == 0){
    document.getElementById(x).value=minutes + minuteslabel + seconds + secondslabel;
    document.getElementById(x).style.backgroundColor = '#E19B9B';
    }
    else if ( days == 0){
    document.getElementById(x).value=hours + hourslabel + minutes + minuteslabel;
    document.getElementById(x).style.backgroundColor = '#9BE1A0';
    }
    else {
    if(document.getElementById(x)){
    document.getElementById(x).value=days + dayslabel + hours + hourslabel;
    document.getElementById(x).style.backgroundColor = '#9BE1A0';
    }
    }
    if ( t < 0 ){
    document.getElementById(x).value='Ended';
    document.getElementById(x).style.backgroundColor = 'transparent';
    document.getElementById(b).value='Ended';
    clearInterval (i);
    }
    }
    

    【讨论】:

    • 我尝试使用你的代码,但是当我集成到我的 php 中时,它不起作用,我有同样的问题,只有在第一行出现了计时器
    • 有人知道如何处理这个问题吗?
    猜你喜欢
    • 2013-03-07
    • 2014-12-18
    • 2020-08-14
    • 1970-01-01
    • 2019-03-14
    • 2019-06-18
    • 2011-11-02
    • 2022-01-15
    • 2020-01-01
    相关资源
    最近更新 更多