EchoFool Posted February 28, 2008 Share Posted February 28, 2008 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 Quote Link to comment Share on other sites More sharing options...
vicodin Posted February 28, 2008 Share Posted February 28, 2008 Is $_POST['Checkbox'] a array? If not foreach is not a good way to copy a variable. Quote Link to comment Share on other sites More sharing options...
EchoFool Posted February 28, 2008 Author Share Posted February 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
btherl Posted February 29, 2008 Share Posted February 29, 2008 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; } } Quote Link to comment Share on other sites More sharing options...
fireMind Posted February 29, 2008 Share Posted February 29, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.