www.webdeveloper.com
Results 1 to 10 of 10

Thread: PHP Loops

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    4

    PHP Loops

    Hey!

    I'm fairly new to php, and I wanted to create a form where if they pressed a button, a loop would start, and it would only stop when they either exited the page, or hit a stop button. I'm not sure how to go about doing this, any advice/help?

  2. #2
    Join Date
    Mar 2013
    Location
    Iasi, Romania
    Posts
    53
    Can you more specific? What that loop should do?

  3. #3
    Join Date
    Mar 2013
    Location
    Iasi, Romania
    Posts
    53
    try something like this:
    put this in a php file
    PHP Code:
    <?php 
    $running_ps       
    shell_exec('ps -afx | grep php');//get the php processes

    if(preg_match_all('/([0-9]+)(.*?)php (201)/'$running_ps$a_matches)) {//identify the proces you need
        
    foreach ($a_matches[1] as $key => $value) {
            
    $a_running_proc $value;
        }
    }
    if (isset(
    $_REQUEST['action'])) {

        if(
    $_REQUEST['action'] == '0' ) {
            
            
    //stop the process
            
    proc_close(proc_open('kill '$a_running_proc, array(), $foo));//kill the process
        
    }
            else
            {
                
    //do whatever you want to do
                //then start the process again
                
    proc_close(proc_open("php [script_file_name] 201 &", array(), $foo));//run the process in background
                
            
    }

    ?>
    and here is the html
    HTML Code:
    <html>
    <body>
    <a href="[script_file_name]?action=1"><button>Start</button></a>
    <a href="[script_file_name]?action=0"><button>Stop</button></a>
    </body>
    </html>

  4. #4
    Join Date
    Mar 2013
    Posts
    4
    Quote Originally Posted by ssdog View Post
    try something like this:
    put this in a php file
    PHP Code:
    <?php 
    $running_ps       
    shell_exec('ps -afx | grep php');//get the php processes

    if(preg_match_all('/([0-9]+)(.*?)php (201)/'$running_ps$a_matches)) {//identify the proces you need
        
    foreach ($a_matches[1] as $key => $value) {
            
    $a_running_proc $value;
        }
    }
    if (isset(
    $_REQUEST['action'])) {

        if(
    $_REQUEST['action'] == '0' ) {
            
            
    //stop the process
            
    proc_close(proc_open('kill '$a_running_proc, array(), $foo));//kill the process
        
    }
            else
            {
                
    //do whatever you want to do
                //then start the process again
                
    proc_close(proc_open("php [script_file_name] 201 &", array(), $foo));//run the process in background
                
            
    }

    ?>
    and here is the html
    HTML Code:
    <html>
    <body>
    <a href="[script_file_name]?action=1"><button>Start</button></a>
    <a href="[script_file_name]?action=0"><button>Stop</button></a>
    </body>
    </html>

    If I did this to the else statement:
    PHP Code:
            else 
            { 
    $i 0;
    echo 
    $i;

                
    proc_close(proc_open("php [script_file_name] 201 &", array(), $foo));//run the process in background
                 
            

    Would that continue to send the email until someone hits stop? If so, at what delay? If not, how can I make it do that?

  5. #5
    Join Date
    Mar 2013
    Posts
    4
    I mean would it continue to echo the variable?

  6. #6
    Join Date
    Mar 2013
    Location
    Iasi, Romania
    Posts
    53
    Yes, it sends until someone stops it, but be carefull: IF YOU CLOSE THE BROWSER, YOU DON'T STOP THE PROCESS
    As about delays, it doesn't have any delay...send the email, then runs the process again.
    If you want to add a delay, you can put the script to sleep by doing this:
    PHP Code:
    else  
            {  
    $i 0
    echo 
    $i
                
    sleep([int_value]);
                
    proc_close(proc_open("php [script_file_name] 201 &", array(), $foo));//run the process in background 
                  
            


  7. #7
    Join Date
    Mar 2013
    Location
    Iasi, Romania
    Posts
    53
    Yet...you won't be able to see that this works by echoing, because it is a process, so it won't refresh your web page...you should write those values into a file or a DB.

  8. #8
    Join Date
    Mar 2013
    Posts
    4
    Quote Originally Posted by ssdog View Post
    Yet...you won't be able to see that this works by echoing, because it is a process, so it won't refresh your web page...you should write those values into a file or a DB.
    How would i check if the script is already running? As in if i want a field on.the first webpage that says "Script status: Running/not running"

  9. #9
    Join Date
    Mar 2013
    Location
    Iasi, Romania
    Posts
    53
    you could do something like
    PHP Code:
    <?php  
    $running_ps       
    shell_exec('ps -afx | grep php');//get the php processes 

    if(preg_match_all('/([0-9]+)(.*?)php (201)/'$running_ps$a_matches)) {//identify the proces you need 
        
    foreach ($a_matches[1] as $key => $value) { 
            
    $a_running_proc $value
        } 

    if (isset(
    $_REQUEST['action'])) { 

        if(
    $_REQUEST['action'] == '0' ) { 
             
            
    //stop the process 
            
    proc_close(proc_open('kill '$a_running_proc, array(), $foo));//kill the process 
           
    echo "process stopped";
            require(
    'html_file_name');
        } 
            else 
            { 
                
    //do whatever you want to do 
                //then start the process again 
                
    proc_close(proc_open("php [script_file_name] 201 &", array(), $foo));//run the process in background 
               
    echo "process running";//print this text before showing buttons
               
    require('html_file_name');//this will show those buttons
                 
            

    }
    ?>

  10. #10
    Join Date
    Mar 2013
    Location
    Iasi, Romania
    Posts
    53
    but if you want to do this check when you first enter the page, yoiu should change the html file, make it php and add this:
    PHP Code:
    <html>
    <body>
    <?
    $running_ps       
    shell_exec('ps -afx | grep php');//get the php processes 

    if(preg_match_all('/([0-9]+)(.*?)php (201)/'$running_ps$a_matches)) 
                                                 {
                                                      echo 
    "running";
                                                   }
                  else {
                            echo 
    "not running";
                           }
    ?>
    <a href="[script_file_name]?action=1"><button>Start</button></a>
    <a href="[script_file_name]?action=0"><button>Stop</button></a>
    </body>
    </html>
    but if you do this, there is no need to change the php as described in the post above...you could just call header('Location:[button_file]'); instead of echo and require()

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles