#1
  1. Learning all I can
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Location
    Pleasanton, California, USA
    Posts
    287
    Rep Power
    11

    Stopping execution to display error message


    During processing of my form data, occasionally while executing a lengthy loop checking values and so forth, sometimes a condition is satisfied where I want to exit the loop (to save clock cycles), cease execution of the script, display the form page (form and process script are all on one big file) and display the error message I wrote to an error array called $tooFewPods.

    I've used this construct with success before:
    Code:
    if(isset($tooFewPods)){
       echo'<ul class="warning">'; 
       foreach($tooFewPods as $item){
          echo'<li>'.$item.'</li>';
       }
       echo'</ul>';
    }
    That would probably work in this case, too. I can place that statement in my XHTML form, surrounded by <?php ... ?> tags, in the location I want the error message to appear.

    However, back in my process loop, I am unclear on how to properly cease loop execution and display the page and error message. Near the tail end of script processing, two redirects automatically take the visitor to either a success or failure page, depending... Unless I disable those two redirect statements, my form page won't generate and I cannot display my error message. I know this is a common task, but it's one of many I have yet to master.

    Can you suggest how I stop script execution and display my error message?

    Thank you very much.
    Curtis Stallins
    Curtis
    - living a newbie's dream
  2. #2
  3. User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Location
    UK
    Posts
    471
    Rep Power
    76
    Assuming you want the for loop to stop after a certain number of cycles, try this, it uses a while so that you only ever count the number you want and then stop. I can't say I could see what you were doing with $tooFewPods here, I'm also assuming $item is an array:
    PHP Code:
    echo'<ul>';
    $i=0;
    $num count($list);
    $stop 50//This is how many cycles you want to limit it to.
    while($i<=$num){
          echo
    '<li>'.$item[$i].'</li>';
          if(
    $i == $stop){
                  
    $end 'y';
                  endwhile; 
    //Stop the loop
           
    }
          
    $i++
    }
    echo
    '</ul>';
    if(
    $end == 'y'){
          echo 
    "Loop stopped..."//Error message is here

    Comments on this post

    • E-Oreo disagrees : (-0) you can't use endwhile like that, use break instead
    • cstallins agrees : I can modify this snippet and make it work in my script.
  4. #3
  5. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,659
    Rep Power
    4126
    use

    break;

    to exit from a loop

    use a variable to store your html output rather than just echo-ing it out.

    this way you can decide what contents to display and when to redirect or not

    eg
    PHP Code:
    for/foreach/while (loopConditions) {
         
    //some code
         
    $passed false;
         if(
    condition) {
              break;
         }
         
    $passed true;
    }

    if(
    $passed) {
         
    //all ok
    } else {
         
    //oops

  6. #4
  7. Banned

    Join Date
    Jul 2004
    Location
    The Mews At Windsor Heights
    Posts
    5,326
    Rep Power
    0
    You could use [PHPNET="trigger_error"]trigger_error()[/PHPNET] to throw your own custom fatal error.

    [PHPNET="exceptions"]Exceptions[/PHPNET] are another possibility.

    Comments on this post

    • cstallins agrees : I learned about trigger_error()
  8. #5
  9. Learning all I can
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Location
    Pleasanton, California, USA
    Posts
    287
    Rep Power
    11
    sephers, Northie and b3n, thank you so much for the tips. That is some good stuff and I'll stew on all of it and see what seems to work best for my situation.

    I know you all have other things you could be doing, so I really appreciate you all helping me out.
    Curtis
    Curtis
    - living a newbie's dream

IMN logo majestic logo threadwatch logo seochat tools logo