3
<? while($row = mysql_fetch_array($result))
{
$result2 = $row['end_time'];    

echo"<td><div id=defaultCountdown></div></td>"; 
?>

<script type=text/javascript>

$(function () 

{
var t = ('<? echo $result2; ?>').split(/[- :]/);
   // Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]); 
$('#defaultCountdown').countdown({until: d});
$('#year').text(austDay.getFullYear());
});
</script>
    <? }
?>

So basically I wanted to launch the Javascript countdown timer for each row of results generated by mysql_fetch_array. However, in the code above, the timer only works for the first row of result. How do I make the timer work for every row of result?

EDIT

   <td> 2012-12-17 13:12:22</td><td> <img src = Images/1324133424.jpg height=200 width=300 />     </td><td> active</td><td><div id=defaultCountdown0></div></td>    
<script type=text/javascript>

$(function () 

{


var t = ('2012-12-17 13:12:22').split(/[- :]/);


// Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]); 

$('#defaultCountdown0').countdown({until: d});

$('#year').text(austDay.getFullYear());
 });


</script>
<td> 2012-12-01 13:12:22</td><td>     <img src = Images/1322754818.jpg height=200 width=300 /> </td><td> active</td>    <td><div id=defaultCountdown1></div></td>    
<script type=text/javascript>

$(function () 

{


var t = ('2012-12-01 13:12:22').split(/[- :]/);


// Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]); 

$('#defaultCountdown1').countdown({until: d});

$('#year').text(austDay.getFullYear());
});


</script>
3
  • You do realize that the PHP side will be long done before the JavaScript even starts? Commented Dec 24, 2011 at 9:36
  • 1
    Please add the output of this PHP script (i.e. The resulting multiple JavaScript snippets). That is what's needed to debug your code. Commented Dec 24, 2011 at 9:38
  • 1
    You're working under the assumption that PHP and JS will execute at the same time, that's completely incorrect. Commented Dec 24, 2011 at 15:45

1 Answer 1

3

In your PHP while loop, you are setting the same ID for all divs, which is probably why only the first row works. However, as I said in my comment, you need to add the actually JavaScript that is generated by your PHP code.

echo"<td><div id=defaultCountdown></div></td>";

EDIT:

Here's what the code should look like. Run it as it is and then try making small changes to get it to do what you want:

<?
    while($row = mysql_fetch_array($result))
    {
        $i = 0;
        $result2 = $row['end_time'];    
        echo "<td><div id=defaultCountdown$i></div></td>"; 
?>

<script type=text/javascript>
    $(function ()
    {
        var t = ('<? echo $result2; ?>').split(/[- :]/);
        // Apply each element to the Date function
        var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]); 
        $('#defaultCountdown<?php echo $i; ?>').countdown({until: d});
        $('#year').text(austDay.getFullYear());
    });
</script>

<?
        $i++;
    }
?>
Sign up to request clarification or add additional context in comments.

12 Comments

Sorry, a newbie here as I'm not sure of the actual Javascript output. From my understanding, it's supposed to pass the values from PHP to Javascript.I followed the usage in this keith-wood.name/countdown.html script. Can you share an example?
The PHP code that you have written writes JavaScript code which is then sent to the browser. What I would like to see is the JavaScript which is now a part of the source of page. On IE, press Alt+v+c to see the page's source. In Chrome and FireFox, press ctrl+u. However, first take care of the ID issue that I pointed out in my reply. You cannot assign the same ID to multiple elements, which is what you are doing in you PHP while loop.
From the Javascript source code, it appears that the Javascript is running correctly. Variable t is also assigned to the respective value from PHP. It's just that there is no output in the browser. You are correct, must be the problem with the multiple same IDs.
However the results are generated dynamically. Does that mean I'll have to put id=defaultCountdown1, id=defaultCountdown2, id=defaultCountdown3, etc? There are many rows generated.
Yes, that's what you have to do. Try something like this: $i = 0; echo"<td><div id=defaultCountdown" . $i . "></div></td>";$i++;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.