php - Javascript 计时器不与 mysql_fetch_array 结果一起循环

标签 php javascript mysql

<? while($row = mysql_fetch_array($result))
{
$result2 = $row['end_time'];    

echo"<td><div id=defaultCountdown></div></td>"; 
?>

<script type=text/javascript>

$(function () 

{
var t = ('<? echo $result2; ?>').split(/[- :]/);
   // Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]); 
$('#defaultCountdown').countdown({until: d});
$('#year').text(austDay.getFullYear());
});
</script>
    <? }
?>

所以基本上我想为 mysql_fetch_array 生成的每一行结果启动 Javascript 倒数计时器。但是,在上面的代码中,定时器只对结果的第一行有效。如何让计时器为每一行结果工作?

编辑

   <td> 2012-12-17 13:12:22</td><td> <img src = Images/1324133424.jpg height=200 width=300 />     </td><td> active</td><td><div id=defaultCountdown0></div></td>    
<script type=text/javascript>

$(function () 

{


var t = ('2012-12-17 13:12:22').split(/[- :]/);


// Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]); 

$('#defaultCountdown0').countdown({until: d});

$('#year').text(austDay.getFullYear());
 });


</script>
<td> 2012-12-01 13:12:22</td><td>     <img src = Images/1322754818.jpg height=200 width=300 /> </td><td> active</td>    <td><div id=defaultCountdown1></div></td>    
<script type=text/javascript>

$(function () 

{


var t = ('2012-12-01 13:12:22').split(/[- :]/);


// Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]); 

$('#defaultCountdown1').countdown({until: d});

$('#year').text(austDay.getFullYear());
});


</script>

最佳答案

在您的 PHP while 循环中,您为所有 divs 设置相同的 ID,这可能就是只有第一行有效的原因。但是,正如我在评论中所说,您需要添加由 PHP 代码生成的实际 JavaScript。

echo"<td><div id=defaultCountdown></div></td>";

编辑:

代码应该是这样的。按原样运行它,然后尝试进行小的更改以使其执行您想要的操作:

<?
    while($row = mysql_fetch_array($result))
    {
        $i = 0;
        $result2 = $row['end_time'];    
        echo "<td><div id=defaultCountdown$i></div></td>"; 
?>

<script type=text/javascript>
    $(function ()
    {
        var t = ('<? echo $result2; ?>').split(/[- :]/);
        // Apply each element to the Date function
        var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]); 
        $('#defaultCountdown<?php echo $i; ?>').countdown({until: d});
        $('#year').text(austDay.getFullYear());
    });
</script>

<?
        $i++;
    }
?>

关于php - Javascript 计时器不与 mysql_fetch_array 结果一起循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8623751/

相关文章:

php - 使用 Composer 安装/更新后是否应该自动从磁盘中删除依赖项?

php - 将mysql字段添加到if语句

javascript - 为什么即使有字符串键,数组也保留了这么多空间?

mysql - 用于 Wordpress 的事件日历插件 - MySQL 查询非常慢?

python - MySQL 连接器/python (v1.1.6) 在查询许多列并使用预准备语句时失败

php - 我想在 mysql 中更新第二个表时自动更新表

php - LAMP 服务器 - 一个命令?

php - 数据库设计 : Matching sql database keys to php constants?

javascript - 查找未销毁的 Vue 组件

javascript - 实现 ng2 文件上传