title: Multiple countdown timers using a PHP loop
body: <p>What I'm trying to do is output data from my database using PHP. Alongside this data I'd like to include a countdown timer using the data from my database. Naturally, the timer is done using JavaScript but I'm struggling to work out how to include the PHP variable in the JavaScript code and then loop through all my results, including an instance of the timer on each row.</p>
  
  <p>I'm testing over here: <a href="http://www.lineswritten.co.uk/Countdown/" rel="nofollow">http://www.lineswritten.co.uk/Countdown/</a> - The red 'timer here' is where I'd want the top 4 instances of 'waiting...' to be. These four instances aren't included in my loop.</p>
  
  <p>I presume this would be done with <code>$count++</code> but I'm not sure how to code this all up.</p>
  
  <p>My JavaScript timer has been grabbed from here: <a href="http://jsfiddle.net/HSx9U/" rel="nofollow">http://jsfiddle.net/HSx9U/</a></p>
  
  <p>The code is the following:</p>
  
  <p>JavaScript</p>
  
  <pre><code>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 &gt; now)
    {
      tdiv.innerHTML = diff +' seconds untill ' + the_event;
    }
    else {clearInterval(to);}
  };
  this.rewriteCounter();
  to = setInterval(this.rewriteCounter,1000);
  </code></pre>
  
  <p>}</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;div id="counter1"&gt;waiting...&lt;/div&gt;
  &lt;div id="counter2"&gt;waiting...&lt;/div&gt;
  &lt;div id="counter3"&gt;waiting...&lt;/div&gt;
  &lt;div id="counter4"&gt;waiting...&lt;/div&gt;
  </code></pre>
  
  <p>HTML Table/PHP loop</p>
  
  <pre><code>&lt;table&gt;
   &lt;tr&gt;
    &lt;th id="logo"&gt;Logo&lt;/th&gt;
    &lt;th id="name"&gt;Name&lt;/th&gt;
    &lt;th id="discount"&gt;Discount&lt;/th&gt;
    &lt;th id="length"&gt;Sale Length&lt;/th&gt;
    &lt;th id="time"&gt;Time Remaining&lt;/th&gt;
    &lt;th id="what"&gt;On What&lt;/th&gt;
    &lt;th id="small-print"&gt;Small Print&lt;/th&gt;
    &lt;th id="link"&gt;Link to Sale&lt;/th&gt;
  &lt;/tr&gt;
  
  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'];
  
    &lt;tr&gt;
    &lt;td headers="logo"&gt;&lt;?php echo $logo;?&gt;&lt;/td&gt;
    &lt;td headers="name"&gt;&lt;p&gt;&lt;?php echo $brand_name;?&gt;&lt;/p&gt;&lt;/td&gt;
    &lt;td headers="discount"&gt;&lt;p&gt;&lt;?php echo $discount;?&gt;&lt;/p&gt;&lt;/td&gt;
    &lt;td headers="length"&gt;&lt;p&gt;&lt;?php echo $length;?&gt;&lt;/p&gt;&lt;/td&gt;
    &lt;td headers="time"&gt;&lt;p style="color:#F00;"&gt;timer here&lt;/p&gt;&lt;/td&gt;
    &lt;td headers="what"&gt;&lt;p&gt;&lt;?php echo $product;?&gt;&lt;/p&gt;&lt;/td&gt;
    &lt;td headers="small-print"&gt;&lt;p&gt;&lt;?php echo $terms;?&gt;&lt;/p&gt;&lt;/td&gt;
    &lt;td headers="link"&gt;&lt;p&gt;Redeem&lt;/p&gt;&lt;/td&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>}
  
  &lt;/table&gt;
  </code></pre>
  
  <p>I presumed I could change the <code>new Date('2010/12/11 10:44:59')</code> to <code>new Date('$newDate')</code> but it didn't work.</p>