DeathStar Posted March 26, 2007 Share Posted March 26, 2007 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? Quote Link to comment Share on other sites More sharing options...
wilorichie Posted March 26, 2007 Share Posted March 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 26, 2007 Share Posted March 26, 2007 what makes you say it cant be done ? surely getting the current time and then displaying the it from within a loop will work! Quote Link to comment Share on other sites More sharing options...
DeathStar Posted March 26, 2007 Author Share Posted March 26, 2007 ok but how? How can I still countdown while not being on the page? Setting up a cron would just be mad!?! Quote Link to comment Share on other sites More sharing options...
wilorichie Posted March 26, 2007 Share Posted March 26, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted March 27, 2007 Share Posted March 27, 2007 You could (if on Linux) use the at command to run the script 5 minutes from whenever. Yiu might want to describe exactly what it is your tryimg to do though. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 27, 2007 Share Posted March 27, 2007 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 Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 27, 2007 Share Posted March 27, 2007 if you wanna have some flash then this may help (just found not reviewd) http://www.flashkit.com/tutorials/Dynamic_Content/Simple_P-Jeffrey_-656/index.php of course its only a start! Quote Link to comment Share on other sites More sharing options...
DeathStar Posted March 27, 2007 Author Share Posted March 27, 2007 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.. Quote Link to comment Share on other sites More sharing options...
mattd8752 Posted March 27, 2007 Share Posted March 27, 2007 Set ignore user abort and set the timeout to 0 Quote Link to comment Share on other sites More sharing options...
trq Posted March 27, 2007 Share Posted March 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
Sephiriz Posted March 27, 2007 Share Posted March 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 27, 2007 Share Posted March 27, 2007 very nice Sephiriz but won't the use have to be active on the page for the javascript to launch the php code ? Quote Link to comment Share on other sites More sharing options...
DeathStar Posted March 27, 2007 Author Share Posted March 27, 2007 Not necessarily. Ill try that way Sephiriz, but i think il do it the old fashion way.. Quote Link to comment Share on other sites More sharing options...
Sephiriz Posted March 28, 2007 Share Posted March 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 28, 2007 Share Posted March 28, 2007 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) Quote Link to comment Share on other sites More sharing options...
Sephiriz Posted March 28, 2007 Share Posted March 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 28, 2007 Share Posted March 28, 2007 LOL I get ya Quote Link to comment Share on other sites More sharing options...
DeathStar Posted March 28, 2007 Author Share Posted March 28, 2007 Or i can put: mysql_query("UPDATE `tabel` SET `time`=UNIX_TIMESTAMP()"); on my main pages and just put a calculation in, but then.. The user must browse and stay on the website. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.