每次while循环运行时调用javascript以从数据库中提供唯一的时间

时间:2013-09-10 11:04:10

标签: php javascript mysql sql

很抱歉再次出现问题,我还有另外一个问题

这是来自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。 其他行工作完美,它们会从数据库中检索所有信息,但是这一行需要显示每行的计时器(倒计时)。为什么?

1 个答案:

答案 0 :(得分:0)

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

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);
}
}