PHP Do while loop - break on press of a button

rankdominator

Junior Member
Joined
Dec 18, 2009
Messages
120
Reaction score
68
Hi,

Need some help with a PHP loop.

I would like the loop to continue indefinately until the user presses a button. The loop can either start immediately or be started with the press of another button. (Sort of like with Start Stop buttons)

Anyone has some PHP syntax for this. Ive searched Google and get VB and C# options, but nothing with PHP that will help me.
Im sure this is easy to do - just pulling my hair out at the mo.

Also not that once the loop starts it should continue without user intervention, and can only be 'broken' with the user click.

Thanks
 
Hmm, I might be wrong but I dont think this can be done with php, since php is all server side and the code is all executed before the page is displayed... someone might want to clarify that but I think thats the case.
 
you need to use ajax which can call execution of a php script
take a look at jquery library
 
PHP:
ob_start();
echo "<a href='#' onclick='window.stop();return false;'>STOP</a>";
ob_flush(); 
flush(); 	
while(true);

simple semi-solution
 
Doing this in php is a bad idea. Give more details and I'm sure better suggestions will follow
 
you can do something like:

PHP:
global $pause;

while(true)
{

if($pause)
    {
       //if pause button pressed sleep 99999999999999 seconds
       echo "I'm going to sleep long time<br>"; 
       sleep(99999999999999);   
    }

//do somthing till the pause button pressed
echo "I'm workin on somthing<br>";
}

you can control $pause from a button or something..u figure it out..
but for a better solution use ajax.
 
You don't want to use PHP for this. Not only will it tie up an available socket connection to your server, PHP execution will automatically end after the maximum execution time has been reached (this is usually 30 seconds on shared hosting).

Do this in Javascript.

Code:
var your_function = function() {
    // check to see if your condition has been satisfied
    if( condition_is_satisfied ) {
        // do whatever you need to do
    } else {
        // otherwise, re-enqueue the job to run 100ms later
        setTimeout("your_function();", 100);
    }
}

your_function();
 
Last edited:
You don't want to use PHP for this. Not only will it tie up an available socket connection to your server, PHP execution will automatically end after the maximum execution time has been reached (this is usually 30 seconds on shared hosting).
[/code]
nope he still can change the execution time to infinity..
 
You could do this in something like Appcelerator Titanium which uses web programming languages to make desktop apps, but for the web only, use javascript. jQuery has an amazing JS library, I suggest you check it out.
 
Back
Top