Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6334951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:45:35+00:00 2026-05-24T18:45:35+00:00

I have divs displayed with a PHP script using foreach. Each div should call

  • 0

I have divs displayed with a PHP script using foreach. Each div should call a JavaScript function and generate a new div unic with id but I don’t know how to increment the series. I need a incrementing loop each time the a div calls the function. Is that possible?

This is the .php file:

<body>
  <script type="text/javascript">
     foreach($objects as $key => $ar1)
      {
        countdown_clock_abs(<?php echo $ar1['timespamSale']; ?>,1);
      }
  </script>
 </body>

</html>

This is the JavaScript:

function countdown_clock_abs(time, format)
{
     // I expected the foreach loop do the job but the php it is loaded before the javascrip is launched. So clearly this is not the way to do it.
     html_code = '<div id="countdown<?php echo ar1['id']; ?>"></div>';

     document.write(html_code);

     countdown_abs(time, format);                
}
function countdown_abs(time, format)
{
var d=new Date();
var Target_Time = (time);
var Now_time = ((d.getTime()));

Time_Left = Math.round(Target_Time-(Now_time)/1000);

     if(Time_Left < 0)
        Time_Left = 0;

     var innerHTML = '';

     switch(format)
           {
           case 0:
                innerHTML = Time_Left + ' seconds';
                break;
           case 1:
                days = Math.floor(Time_Left / (60 * 60 * 24));
                Time_Left %= (60 * 60 * 24);
                hours = Math.floor(Time_Left / (60 * 60));
                Time_Left %= (60 * 60);
                minutes = Math.floor(Time_Left / 60);
                Time_Left %= 60;
                seconds = Time_Left;

                dps = 's'; hps = 's'; mps = 's'; sps = 's';

                if(days == 1) dps ='';
                if(hours == 1) hps ='';
                if(minutes == 1) mps ='';
                if(seconds == 1) sps ='';

                innerHTML = days + ' day' + dps + ' ';
                innerHTML += hours + ' hour' + hps + ' ';
                innerHTML += minutes + ' minute' + mps + ' and ';
                innerHTML += seconds + ' second' + sps;
                break;
                default: innerHTML = Time_Left + ' seconds';
           }                   

        document.getElementById('countdown<?php echo ar1['id']; ?>').innerHTML = innerHTML;     

        setTimeout('countdown_abs(' + time + ',' + format + ');', 50);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:45:36+00:00Added an answer on May 24, 2026 at 6:45 pm

    Modify the signature of your method to match function countdown_clock_abs(time, format, id). Now in your first block of code, you can pass the counter to the id param

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Divs show and hide (with an animation) using the following script (I
I have a script which hides (display:none) certain divs in the list on page
We have an HTML page which displays a bunch of pretty bars using divs
I have three divs : <div id=login /> <div id=content /> <div id=menu />
I have three DIVs, something like this: <div id=header> ... </div> <div id=content> <div
I have 3 divs with 2 possible image tags for each (active or inactive).
I have multiple toggle divs on the same page, and the div I'm toggling
i have two divs on a page. each has several dijits. i have atoggle
I am using content editable divs and I have overflow set to hidden. In
I have a few div boxes that are using position:fixed and I use a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.