Jump to content

Live Count


DeathStar

Recommended Posts

Hi there.

I am just wondering if this can be done:

That php can how say a count down like say I want t orun a countdown but live to the user say from 5 minutes to 0.

And if it hits zero it updates something in the database.

It must also keep trackof each second so th euser does not have to be on the page for 5 minutes, it can just contunue.

 

How can this be done in php?

Link to comment
Share on other sites

You can't do that with PHP. Although you could write something in JavaScript maybe, so it will count down, when it hits 0, it will ping/refresh the page. On refresh, you would do the update.

 

This would mean you MUST be on the page the whole time though.

 

To ping the page while not being on it, you will need another client and software to ping the page at a certain time

Link to comment
Share on other sites

what makes you say it cant be done ?

 

surely getting the current time and then displaying the it from within a loop will work!

 

PHP is parsed on request

 

To do what you want to do will mean you need to setup a client to ping/refresh your page at a certain time/s.

Link to comment
Share on other sites

 

PHP is parsed on request

 

To do what you want to do will mean you need to setup a client to ping/refresh your page at a certain time/s.

 

compared to client side javascript.. erm. .yeah right

 

 

 

i would probably have the cron job to start the process so you don't have to be on the page to make it run, the script would need to update a file/database another script would read the data for the count down this could be javascript or just a loop in php your need to use sleep or something or your kill the CPU

Link to comment
Share on other sites

this is what i want to  do:

There is a place on the site where you click an form submit button, when clicked it turns top the time countdown.

Then i want it to have to be able to do live count down on the progress. and when finished replace the tiem again with a form button.

It has to be able to countdown even when not on the page.

 

Hope that helps..

Link to comment
Share on other sites

If you mean you want the client to be able to click a button, have the 5 min countdown start, close the page, come back 2 mins later and see 3 mins remeaning on the countdown, then your dreaming. http isn't really a good tech for such things. While it may be possible, its not something I would dare spend any time developing a solution for.

 

If you want your users to click a button which instigates an action to take place in 5 minutes regardless of your users still being present or not, that is possible. Id'e use at.

Link to comment
Share on other sites

Alright, from a hypothetical standpoint, I would say your solution is AJAX.

 

Step by step, this is how I imagine what you want to happen would work:

 

1.  User hits form button.  When that button is hit, trigger the execution of a PHP script.  In this instance, your PHP script will check a database field whether a time for the user has been set or not.  If not, then you store the current time.

 

2.  Have the AJAX code get the stored time from the php script, and then get the current time.  Through some basic math you should get the amount of time that has elapsed.  From there on, just make a simple javascript countdown timer.  The great part about this is that if the user refreshes the page, it won't influence the fact that the time they hit submit is still stored in a database.

 

3.  If the javascript timer counts down to zero, execute another PHP script that will remove the time from the database.

 

4. Since the page that the form button corresponds to is probably PHP, just make sure that the code on that page cannot be executed if a time is set in the database.

 

This is all theoretical, but its definitely possibly.  Not easy, but doable.

 

 

Link to comment
Share on other sites

very nice Sephiriz  but won't the use have to be active on the page for the javascript to launch the php code ?

 

Well, that's the idea.  At the moment the user hits the submit button, you start the timer.  It doesn't matter if he's on the page or not, the timestamp limits him regardless, because you check every time you load the page.  The timer is just superficial, and once the user can hit the form button again, the javascript will just re-enable the button.

Link to comment
Share on other sites

I mean with

 

3.  If the javascript timer counts down to zero, execute another PHP script that will remove the time from the database.

 

Surely if the time hits 0 and the user isn't on the page it will just wait until the use returns ?

 

i'm useless at javascript but from my understanding i didn't think it would work (its still a solution)

Link to comment
Share on other sites

Well he just want the person to be able to use the form button again, so they have to be on the page.  You just do all the code on the loading of the page, since he has to come back to the site anyway.  I don't know if I'm clear, lol, so I'm sorry, bear with me.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.