www.webdeveloper.com
Results 1 to 3 of 3

Thread: How can I create only one function

  1. #1
    Join Date
    Dec 2011
    Posts
    5

    How can I create only one function

    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";
        
    $resultmysql_query($query);
        
    $count=0;
        
    $end_time= array();
        
        while( 
    $recordmysql_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'";
            
    $result2mysql_query($query);
            
    $recordmysql_fetch_object($result2);
            
    $time$record->end_time;

            
    date_default_timezone_set('Asia/Singapore');
            
            
    $end_time[$count]= strtotime($time); //unix_time
            
            
            
            
    $nowtime(); //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>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    may be this will help

    Code:
    function countdown(endtime,objID){
    var now=new Date(),unix_now=now.getTime()/1000;
    unix_now=Math.floor(unix_now);
    var sec=endtime-unix_now;
    if(sec>0){
    var min=Math.floor(sec/60),hour=Math.floor(min/60);
    hour%=24;min%=60;sec%=60;
    document.getElementById(objID).innerHTML= hour+":"+min+":"+sec;
    setTimeout('countdown('+endtime+',"'+objID+'")',999);
    }
    else{return;}
    }
    
    countdown(<?php echo $end_time[0]; ?>,'0');
    countdown(<?php echo $end_time[1]; ?>,'1');
    countdown(<?php echo $end_time[2]; ?>,'2');
    
    // by the way, the id attribute must not start from a digit
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Dec 2011
    Posts
    5
    Hi thanks for your help. I actually have to solve already. Someone from codingforum helped me out.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles