I have a page that displays multiple product, each with its own timer. You can view it here.
Problem is I do not know how to create a loop for the countdown function. It has problem when I do so. So I create 3 separate function for each product. The thing is this way is not dynamic. Please help.
PHP Code:<?php
include "connect.php";
$query= "select * from product";
$result= mysql_query($query);
$count=0;
$end_time= array();
while( $record= mysql_fetch_array($result) ){
$product_id= $record['product_id'];
$product_name= $record['product_name'];
$retail_price= $record['retail_price'];
$product_price= $record['product_price'];
echo '<div id="product">';
echo '<div>'.$product_name.'</div>';
echo '<img style="width: 90%;" src="images/'.$product_id.'.png"/>';
echo 'Retail Price';
echo '<div>'.$retail_price.'</div>';
echo 'Time Left';
$query= "select * from product where product_id='$product_id'";
$result2= mysql_query($query);
$record= mysql_fetch_object($result2);
$time= $record->end_time;
date_default_timezone_set('Asia/Singapore');
$end_time[$count]= strtotime($time); //unix_time
$now= time(); //unix time
$sec= $end_time[$count] - $now;
if($sec<=0){
echo 'something';
}
$min= $sec/60;
$hour= $min/60;
$hour %= 24;
$min %= 60;
$sec = $sec % 60;
echo '<br/><div id="'.$count.'" style="display: inline-block;">'.$hour.':'.$min.':'.$sec.'</div>
<br/>';
echo 'Current Price';
echo '<div>RM '.$product_price.'</div>';
echo '<input type="submit" id="bid" value="Bid"/>';
echo '</div>';
$count++;
}
?>Code:<script type="text/javascript"> setInterval('countdown()',1000); function countdown(){ var now = new Date(); var unix_now= now.getTime()/1000; unix_now= Math.floor(unix_now); var sec = <?php echo $end_time[0]; ?> - unix_now; if(sec<=0){ clearInterval(stop); } var min = Math.floor(sec / 60); var hour = Math.floor(min / 60); hour %= 24; min %= 60; sec %= 60; document.getElementById(0).innerHTML= hour+":"+min+":"+sec; } setInterval('countdown1()',1000); function countdown1(){ var now = new Date(); var unix_now= now.getTime()/1000; unix_now= Math.floor(unix_now); var sec = <?php echo $end_time[1]; ?> - unix_now; if(sec<=0){ clearInterval(stop); } var min = Math.floor(sec / 60); var hour = Math.floor(min / 60); hour %= 24; min %= 60; sec %= 60; document.getElementById(1).innerHTML= hour+":"+min+":"+sec; } setInterval('countdown2()',1000); function countdown2(){ var now = new Date(); var unix_now= now.getTime()/1000; unix_now= Math.floor(unix_now); var sec = <?php echo $end_time[2]; ?> - unix_now; if(sec<=0){ clearInterval(stop); } var min = Math.floor(sec / 60); var hour = Math.floor(min / 60); hour %= 24; min %= 60; sec %= 60; document.getElementById(2).innerHTML= hour+":"+min+":"+sec; } </script>


Reply With Quote
Bookmarks