1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Do while loop - break on press of a button

Discussion in 'PHP & Perl' started by rankdominator, Apr 14, 2010.

  1. rankdominator

    rankdominator Junior Member

    Joined:
    Dec 18, 2009
    Messages:
    117
    Likes Received:
    67
    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
     
  2. homenet

    homenet Power Member

    Joined:
    Jan 5, 2009
    Messages:
    790
    Likes Received:
    338
    Location:
    Dimension X
    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.
     
  3. stoned raider

    stoned raider Registered Member

    Joined:
    Aug 4, 2008
    Messages:
    94
    Likes Received:
    22
    you need to use ajax which can call execution of a php script
    take a look at jquery library
     
  4. cyklotrial

    cyklotrial Regular Member

    Joined:
    Oct 13, 2008
    Messages:
    248
    Likes Received:
    82
    Location:
    Wonderland
    PHP:
    ob_start();
    echo 
    "<a href='#' onclick='window.stop();return false;'>STOP</a>";
    ob_flush(); 
    flush();     
    while(
    true);
    simple semi-solution
     
  5. ThePost

    ThePost Registered Member

    Joined:
    Jun 10, 2009
    Messages:
    71
    Likes Received:
    11
    Doing this in php is a bad idea. Give more details and I'm sure better suggestions will follow
     
  6. xenon2010

    xenon2010 Regular Member

    Joined:
    Apr 27, 2010
    Messages:
    231
    Likes Received:
    48
    Occupation:
    web and desktop apps programmer
    Location:
    prison
    Home Page:
    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.
     
  7. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front
    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: May 29, 2010
  8. xenon2010

    xenon2010 Regular Member

    Joined:
    Apr 27, 2010
    Messages:
    231
    Likes Received:
    48
    Occupation:
    web and desktop apps programmer
    Location:
    prison
    Home Page:
    nope he still can change the execution time to infinity..
     
  9. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front
    This is a really bad idea. :-/

    He probably doesn't have access to change it, anyways.
     
  10. Killswitch

    Killswitch BANNED BANNED

    Joined:
    Aug 9, 2009
    Messages:
    14
    Likes Received:
    5
    Uh, you don't wanna do that via PHP.
     
  11. tacopalypse

    tacopalypse Executive VIP Jr. VIP Premium Member

    Joined:
    Nov 30, 2009
    Messages:
    980
    Likes Received:
    2,485
    Home Page:
    yea.. do it with javascript, not php.
     
  12. andyh2

    andyh2 Junior Member

    Joined:
    Dec 19, 2009
    Messages:
    135
    Likes Received:
    42
    Location:
    Bay Area, CA
    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.