0

Firtly, i select all data time from database, but when show to the page only the first one items will show the timer, other items cannot to show the timer.

 <?php
     $stmtAuction = $db->prepare("SELECT * FROM auction WHERE expired_at >= CURDATE() AND status = 'available' ORDER BY expired_at ASC");
                    $stmtAuction->execute();
     if ($stmtAuction->rowCount() > 0) {
                        while ($item = $stmtAuction->fetch()) {
       ?>
          <span id='countdown' value='<?php echo $item['expired_at'];?>'></span>       

<?php
}
?>
    <script>

     $("#countdown").countdown($('#countdown').attr('value'), function(event) {
                $(this).text(
                    event.strftime('%D days %H:%M:%S')
                );
            });
    </script>

how can i show timer for every items??

4
  • 1
    Well you have $stmtAuction->rowCount() number of id='countdown'. ids are supposed to be unique. So javascript/jQuery will only find the 1st/unique id='countdown'. You need to use class='countdown', and $(".countdown"), although you will need to make some additional changes to make this work. Commented Jan 5, 2016 at 3:10
  • 2
    it works...but all the timer display same time and date Commented Jan 5, 2016 at 3:25
  • 2
    how can i display timer based on my database select? Commented Jan 5, 2016 at 3:26
  • Have added an answer that shows changing from id to class, as well as how to use $(".countdown") and $.each() to display each timer data. Commented Jan 5, 2016 at 3:36

1 Answer 1

2

Your issue is caused by un-unique ids. You have $stmtAuction->rowCount() number of id='countdown'. ids are supposed to be unique. So javascript/jQuery will only find the 1st/unique id='countdown'. You need to use class='countdown' instead -

<?php
    $stmtAuction = $db->prepare("SELECT * FROM auction WHERE expired_at >= CURDATE() AND status = 'available' ORDER BY expired_at ASC");
    $stmtAuction->execute();
    if ($stmtAuction->rowCount() > 0) {
        while ($item = $stmtAuction->fetch()) {
        ?>
            <span class='countdown' value='<?php echo $item['expired_at'];?>'></span>
                  ^^^^^-change from id to class
<?php
        }
    }
    ?>

Now in your javascript, you need to use the class selector - $(".countdown") - and loop over each class using $.each().

<script>
    $('.countdown').each(function(){
        $(this).countdown($(this).attr('value'), function(event) {
            $(this).text(
                event.strftime('%D days %H:%M:%S')
            );
        });
    });
</script>

a jsFiddle example - https://jsfiddle.net/smh5nhgz/1/

Sign up to request clarification or add additional context in comments.

1 Comment

hi sean , why when i put today date for example , value='2016/01/05 05:00:00' , then the countdown become 00 days 00:00:00...how to fix it?

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.