0

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

  }
 ?>
Community
  • 1
  • 1
Irene Ling
  • 1,881
  • 3
  • 26
  • 41
  • 1
    As an aside - you are not doing any error checking in your query. You *need* to do that after a `mysql_query()` call. Otherwise, your script will break if the query fails. How to do this is outlined in the [manual on `mysql_query()`](http://php.net/mysql_query) or in this [reference question.](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – Pekka Dec 22 '11 at 23:24
  • btw, I edited your code and there was a right curly bracket missing for your while loop, I'm assuming that was human error when you copy pasted. – puk Dec 22 '11 at 23:27
  • @puk Thanks but I did put right curly bracket at the end of the script. – Irene Ling Dec 22 '11 at 23:36
  • oops, so sorry about that, revert back my edit then – puk Dec 22 '11 at 23:37

1 Answers1

2

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"

Fritz
  • 186
  • 2
  • 1
    @puk the bracket was correct! note its closed in the last line. (I cannot write a comment to the question post) – Fritz Dec 22 '11 at 23:30
  • Thank you Fritz!It works.I see my error now,thank you so much , I learn something new again. – Irene Ling Dec 22 '11 at 23:34