简体   繁体   中英

Display multiple live countdown using data retrieved from database

I have multiple time countdowns that need to get displayed on one page and the time left is data from a database.

I'm using examples from another website, and I tried many of them, but I can only display one.

My code:

 <?
      $result = mysql_query("SELECT * FROM `time` ");
      while ($row = mysql_fetch_array($result)) {
           $futuredate = date('F d, Y H:i:s', time()+$row['TimeLeft']);

 ?>

 <div id="cdcontainer"></div>
 <script type="text/javascript">
      var launchdate = new cdLocalTime("cdcontainer", "server-php", 0, "<?php echo $futuredate;?>", "debugmode")
      launchdate.displaycountdown("days", formatresults)
 </script>

 <?php

  }
 ?>

I'm not familiar with cdLocalTime but one error I see in your code is that you add two divs with the same id. It's not pretty, but try:

$i = 0;
while ($row = mysql_fetch_array($result)) {
    $futuredate = date('F d, Y H:i:s', time()+$row['TimeLeft']);
?>
<div id="cdcontainer_<?=$i?>"></div>
<script type="text/javascript">
var launchdate=new cdLocalTime("cdcontainer_<?=$i++?>", "server-php", 0, "<?= $futuredate;?>", "debugmode")
launchdate.displaycountdown("days", formatresults)
</script>

<?php }

So you will have cdcontainer_0 and cdcontainer_1 for those 2 times. Just in case "<?=" doesnt work its the same as "<? echo"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM