使用PHP循环的多个倒计时计时器
我正在尝试使用PHP从数据库输出数据。除了这些数据之外,我还想使用数据库中的数据包括一个倒计时计时器。当然,计时器是使用JavaScript完成的,但我正在努力解决如何在JavaScript代码中包含PHP变量,然后循环遍历所有结果,包括每行的计时器实例 我在这里测试:-红色的“timer here”是我想要的“waiting…”的前4个实例。这四个实例不包括在我的循环中 我假设这将通过$count++完成,但我不确定如何编写所有代码 我的JavaScript计时器已从此处抓取: 代码如下: JavaScript使用PHP循环的多个倒计时计时器,php,javascript,loops,countdown,Php,Javascript,Loops,Countdown,我正在尝试使用PHP从数据库输出数据。除了这些数据之外,我还想使用数据库中的数据包括一个倒计时计时器。当然,计时器是使用JavaScript完成的,但我正在努力解决如何在JavaScript代码中包含PHP变量,然后循环遍历所有结果,包括每行的计时器实例 我在这里测试:-红色的“timer here”是我想要的“waiting…”的前4个实例。这四个实例不包括在我的循环中 我假设这将通过$count++完成,但我不确定如何编写所有代码 我的JavaScript计时器已从此处抓取: 代码如下: J
var count1 = new countDown(new Date('2010/12/11 10:44:59'),'counter1', 'tomorrow 10:45')
,count2 = new countDown(new Date('2010/12/25'),'counter2', 'first christmas day 2010')
,count3 = setTimeout(function(){
return new countDown(new Date('2011/12/25'),'counter3', 'first christmas day 2011');
},2000)
,count4 = setTimeout(function(){
return new countDown(new Date('2100/01/01'),'counter4', 'a new era starts');
},4000);
function countDown(startTime, divid, the_event){
var tdiv = document.getElementById(divid)
,start = parseInt(startTime.getTime(),10)
,the_event = the_event || startTime.toLocaleString()
,to;
this.rewriteCounter = function(){
var now = new Date().getTime()
,diff = Math.round((start - now)/1000);
if (startTime > now)
{
tdiv.innerHTML = diff +' seconds untill ' + the_event;
}
else {clearInterval(to);}
};
this.rewriteCounter();
to = setInterval(this.rewriteCounter,1000);
}
HTML
HTML表/PHP循环
<table>
<tr>
<th id="logo">Logo</th>
<th id="name">Name</th>
<th id="discount">Discount</th>
<th id="length">Sale Length</th>
<th id="time">Time Remaining</th>
<th id="what">On What</th>
<th id="small-print">Small Print</th>
<th id="link">Link to Sale</th>
</tr>
while ($row = mysql_fetch_array($result)) {
$discount = $row['discount'];
$product = $row['product'];
$terms = utf8_decode($row['terms']);
$brand_name = $row['brand_name'];
$code = $row['code'];
$link = $row['link'];
$logo = $row['logo'];
$length = $row['length'];
<tr>
<td headers="logo"><?php echo $logo;?></td>
<td headers="name"><p><?php echo $brand_name;?></p></td>
<td headers="discount"><p><?php echo $discount;?></p></td>
<td headers="length"><p><?php echo $length;?></p></td>
<td headers="time"><p style="color:#F00;">timer here</p></td>
<td headers="what"><p><?php echo $product;?></p></td>
<td headers="small-print"><p><?php echo $terms;?></p></td>
<td headers="link"><p>Redeem</p></td>
我假设我可以将新日期2010/12/11 10:44:59改为新日期“$newDate”,但没有成功。从PHP到javascript的内容最好使用PHP的json_encode函数。您可以向其提供如下数组:
$array = ('item1', 'item2');
echo '<script type="text/javascript">';
echo 'var myarray = '.json_encode($array).';';
// iterate etc here
echo '</script>';
我不完全确定在哪里实施这个。数组是否如下所示$数组=$dbTime;恐怕我不习惯使用数组。请在while循环中构建数组,这样您就有了$array['counter'.$rownumber]=$row['time'];然后,当您将数组转换为javascript时,它将显示为JSON为关联数组自动执行的对象,因此myarray.counter1将被设置为您想要为counter1设置的时间。然后,您可以在javascript中循环数组以添加计数器小部件:对于myarray中的var countername{var newcounter=new countdown new Datemyarray[countername],countername,'a new era start';}对于代码格式,很抱歉!我真的很感谢你的帮助,但我完全陷入了一片混乱之中!我对需要保留哪些代码位以及需要替换哪些代码位感到困惑。。
}
</table>
$array = ('item1', 'item2');
echo '<script type="text/javascript">';
echo 'var myarray = '.json_encode($array).';';
// iterate etc here
echo '</script>';