Jump to content

Errors in for loops


EchoFool

Recommended Posts

I need help with a for loop using validation..

 

I have a check on the forloop to make sure all inputs match the requirements but if they are not i want it to show an error which i can do but if there was like 8 inputs then itll end up showing:

There was an error

There was an error

There was an error

x8

 

So was wondering is there a way to cancel the forloop when the error occurs and show only "one error". I can't be using header re-directs, become my main global include contains the title and headers...so output has already started.

 

 

<?php
include("include.php");

foreach ($_POST['Checkbox']  as $Value => $ItemID){

      $Quantity = mysql_real_escape_string($_POST['Quantity'][$Value]);                
If($Value == '' OR $Quantity == '' OR !(is_numeric($Quantity)) OR !(is_numeric($Value))){
?>
<p align="center"><br><br>
<span class="NegativeMoney">There was an error!</span><br><br><br>
<a href="myitems.php?choice">Back</a></p><br><br><br><br>
<?php
   }
}

 

This is what i got, hope you can understand what i am on about :P

Link to comment
https://forums.phpfreaks.com/topic/93587-errors-in-for-loops/
Share on other sites

I'm using the checkbox as part of the array selection as below:

 

<input type="text" size="16" name="Quantity[<?=$Count?>]" value=""></td>
<input type="checkbox" name="Checkbox[<?=$Count?>]" value="<?=$ItemID?>">

 

Barand helped me with this part a while back. Which works, just need to find a way to make the error output only show once so it looks more professional in the for loop.

Link to comment
https://forums.phpfreaks.com/topic/93587-errors-in-for-loops/#findComment-479593
Share on other sites

How about this:

 

<?php
include("include.php");

foreach ($_POST['Checkbox']  as $Value => $ItemID){

      $Quantity = mysql_real_escape_string($_POST['Quantity'][$Value]);                
If($Value == '' OR $Quantity == '' OR !(is_numeric($Quantity)) OR !(is_numeric($Value))){
?>
<p align="center"><br><br>
<span class="NegativeMoney">There was an error!</span><br><br><br>
<a href="myitems.php?choice">Back</a></p><br><br><br><br>
<?php
   break;
   }
}

Link to comment
https://forums.phpfreaks.com/topic/93587-errors-in-for-loops/#findComment-479645
Share on other sites

Yea, I think what you're asking about is basically the break command. In any for, foreach, do-while, while, or switch statements, break can be used to break out of the loop.

 

So you might do something like...

<?php
foreach ($whatever  as  $thing) {
    if ($theres_an_error) {
        $errors[] = "There was an error!";
        break;
    }
}

if (count($errors) > 0) {
    foreach ($errors  as  $error)
        echo "There was an error processing your request:<ul><li>$error</li></ul>";
}
?>

 

Cheers  ;)

Link to comment
https://forums.phpfreaks.com/topic/93587-errors-in-for-loops/#findComment-479659
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.