Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript timer not looping along with mysql_fetch_array results

<? 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>
like image 799
user1033038 Avatar asked Mar 16 '26 14:03

user1033038


1 Answers

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++;
    }
?>
like image 104
Abbas Avatar answered Mar 19 '26 02:03

Abbas