www.webdeveloper.com
Results 1 to 3 of 3

Thread: trying to break out of loop once an error...

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    trying to break out of loop once an error...

    Can someone recommend a way to break out of this for loop if an error gets put into the array error? Currently what happens is, if a bad url is entered (hopefully) that error get repeated until the for loop finishes. I'd prefer that as soon as 1 error gets entered into the array, it would then exit the for loop it is nested in.

    i've been trying to set something up using break, but haven't yet succeeded.,

    thanks for any help on this,

    i can supply a url if necessary, thanks

    PHP Code:
    if(isset($_POST['submit'])) {
        
    $manId $_POST['manualid'];
        
    $url $_POST['url'];
        
    array_walk_recursive($_POSTcreate_function('&$value, $key','$value = trim($value);' )); 
        for(
    $i=0;$i<$count;$i++) {
            
    $manId[$i] = prep_num($manId[$i]);
            
    $url[$i] = prep_url($url[$i]);
            if(!
    isValidURL($url[$i])) {
                
    array_push($errArrUpdate$url[$i].' is not a valid URL');
              
    //BREAK OUT OF LOOP HERE, ONCE AN ERROR OCCURS
            

        if(empty(
    $errArrUpdate)) {
            
    $sql "UPDATE $tbl_name SET manid='$manId[$i]', url='$url[$i]' WHERE id=$id[$i]";
            
    //$result = mysql_query($sql);
            
            
    if(!$result) {
                
    array_push($errArrUpdatemysql_error());
            } else {
                
    //echo 'Update succeeded';
            
    }
        } else {
            
    display_errors($errArrUpdate);
        }
        } 

  2. #2
    Join Date
    May 2012
    Posts
    37
    PHP Code:
    //if you're in a function
    return array_push($errArrUpdate$url[$i].' is not a valid URL');

    // if not, you could use
    array_push($errArrUpdate$url[$i].' is not a valid URL');
    break; 
    could even use exit() if you want everything to stop... return would be the recommended.

    Keep in mind though, the way your code is setup, it's never going to get to checking the state of $errArrUpdate if you break the loop.

    PHP Code:

      
    for($i=0;$i<$count;$i++) {
            
    $manId[$i] = prep_num($manId[$i]);
            
    $url[$i] = prep_url($url[$i]);
            if(!
    isValidURL($url[$i])) {
             
    array_push($errArrUpdate$url[$i].' is not a valid URL');
             break;
            } 
        } 
    // End for loop

        
    if(empty($errArrUpdate)) {
            
    $sql "UPDATE $tbl_name SET manid='$manId[$i]', url='$url[$i]' WHERE id=$id[$i]";
            
    //$result = mysql_query($sql);
            
            
    if(!$result) {
                
    array_push($errArrUpdatemysql_error());
            } else {
                
    //echo 'Update succeeded';
            
    }
        } else {
            
    display_errors($errArrUpdate);
        } 
    Last edited by g0dzuki99; 05-24-2012 at 09:30 AM.

  3. #3
    Join Date
    Mar 2010
    Posts
    281
    thanks, i thought i'd tried break, but yes that works., thanks

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