views:

36

answers:

1

I found the count down script in http://www.dynamicdrive.com/dynamicindex6/dhtmlcount.htm

It works fine if i just define the date but when I try to pass the date from this php/mysql query into the js file, it works only for the first record and not after that.

<script type="text/javascript" src ="time.js"></script>//this is the script from the above link. this directly gets todays date inside this code

<?php
$result = mysql_query("SELECT * FROM my_table LIMIT 10") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$id = $row['id'];
$title = $row['title'];
$date = date("F d, Y", strtotime($row['date']));

 echo '<tr><td>';
 echo $title;
 echo "</td><td>";
 echo $id;
 echo "</td><td>";
 echo $date; //display the date directly from the table as June 25, 2010
 echo "</td><td>";
 ?>
    <!-- this js code and the div is to show the countdown from today like 3D 2H 5M 45-->
 <div id="container"></div>
 <script type="text/javascript">
  var futuredate=new cdtime("container", "<?php echo $date ;?>")
  futuredate.displaycountdown("days", formatresults)
 </script>
 <?php

 echo "</td></tr>"; 
 }

} 
?>

I tried wrapping the js code as php by using echo but it doesnt show anything(not even the output for first record). The above code clearly does what it has to do because I closed that php and opened the js so that's why its not looping. but I am guessing there should be away.

Please help me out.

+2  A: 

Each one of your divs needs to have a unique id. You are also going to run into issues because you are redefining futuredate over and over.

Something like the below should work because each div gets its own id, (container1, container2, container3, etc.) and you are creating a new variable with a different name for each instance of cdtime (futuredate1, futuredate2, futuredate3, etc.). This would not necessarily be my favorite method for doing this, as using something like jQuery's .each() syntax would probably be better (or whatever js framework you choose) rather than defining a bunch of new variables.

I have not tested this code, but this should work:

<script type="text/javascript" src ="time.js"></script>//this is the script from the above link. this directly gets todays date inside this code

<?php
$result = mysql_query("SELECT * FROM my_table LIMIT 10") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
  $id = $row['id'];
  $title = $row['title'];
  $date = date("F d, Y", strtotime($row['date']));

  echo '<tr><td>';
  echo $title;
  echo "</td><td>";
  echo $id;
  echo "</td><td>";
  echo $date; //display the date directly from the table as June 25, 2010
  echo "</td><td>";
?>
    <!-- this js code and the div is to show the countdown from today like 3D 2H 5M 45-->
 <div id="container<?=$id?>"></div>
 <script type="text/javascript">
  var futuredate<?=$id?>=new cdtime("container<?=$id?>", "<?= $date?>")
  futuredate<?=$id?>.displaycountdown("days", formatresults)
 </script>
<?php

 echo "</td></tr>"; 
 }

} 
?>

In regard to your question in the comments about jQuery, you could do something similar to:

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#container<?=$id?>').countdown({until: new Date(YYYY, MM, DD), format: 'D'});
    });
</script>

Note that this is using this plugin, but there are others out there, and you would need to replace the YYYY, MM, DD values with their appropriate values.

cmendoza
thank you cmendoza, it works! Can you please explain me how this is working after you pass the id? I am not getting the logic. It will be good to learn. thanks again for this help!!
Jay
The reason is that each div element now has its own id, as well as each futuredate variable.when parsing the DOM, javascript requires that each element have its own unique ID, so you wouldn't want 2 <div> elements with id="container". Also, you were overwriting the futuredate variable each time you defined it in your code (If you look in the newly generated code, you will see var futuredate{$id} definitions for each div).
cmendoza
thank you cmendoza, nice clarification. if i can ask one more question, what is the way to compare the two dates i have in the above code? basically i want to show the result only if $date is today or its some date in future and not for the dates passed. this one $date = date("F d, Y", strtotime($row['date'])); from db and $today = date("F d, Y"); is todays date. I wan to do if $date>= $today but now my ouput are string type(june 26, 2010) so i can't get the output. is there any way?. thanks.
Jay
also, do you have any example link for the jquery method to achieve this countdown timer?
Jay
You could eliminate them from your sql result (which would probably be the most efficient), by changing your sql statement to SELECT * FROM my_table WHERE date >= CURDATE() LIMIT 10.
cmendoza
See above for an example in jQuery
cmendoza
Thank you for your help. I went with the regular Js method as I was seeing just a blank page using that jquery plug in. Just one last question, with my current implementation, will it show time based on the server or the users computer. Might be a basic question but I saw the time represented differently on two computers from same time zone.
Jay