how to increment a javascript variable title that is within a php while loop
- by steve
I'm building multiple countdown clocks on one page. The number of countdown clocks varies from day to day so I need to call javascript several times from within "while" code in php to produce different clocks. The following code works but it's based on knowing how many clocks are needed before I start:
<script language="javascript" src="countdown.js"></script>
<script language="javascript">
var cd1 = new countdown('cd1');
cd1.Div = "clock1";
cd1.TargetDate = "<?php echo "$clocktime"; ?>";
cd1.DisplayFormat = "%%D%% days, %%H%% hours, %%M%% minutes, %%S%% seconds until event AAA happens";
</script>
<div id="clockwrapper"><div id="clock1">[clock]</div></div>
<script language="javascript" src="countdown.js"></script>
<script language="javascript">
var cd2 = new countdown('cd2');
cd2.Div = "clock2";
cd2.TargetDate = "02/01/2011 5:30:30 PM";
cd2.DisplayFormat = "%%D%% days, %%H%% hours, %%M%% minutes, %%S%% seconds until event BBB happens...";
</script>
<div id="clockwrapper"><div id="clock2">[clock]</div></div>
So if I keep on calling the javascript above (the code with cd1 in it) all previous "cd1" clocks change to the latest clock because it is being overwritten. Somehow I need to call javascript from within my "while" loop in php and have cd1 become cd2, then cd3 so that the clocks work as they're supposed to.
How do I go about doing this? I don't know how to call the javascript several times and increment the variable cd1 within the javascript. I tried something like this but couldn't get it to work.
$id=mysql_result($result,$i,"id");
while($id){
$cd = ("$cd"."$id");
?>
<script language="javascript" src="countdown.js"></script>
<script language="javascript">
var <?php echo "$cd"; ?> = new countdown('<?php echo "$cd"; ?>');
....
</script>
<div id="clockwrapper"><div id="<?php echo "$cd"; ?>">[clock]</div></div>
<?php
$id=mysql_result($result,$i,"id");
}
?>
Surely there is some easy way of getting around this that I don't know about.
Thanks