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($_POST, create_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 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);
"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools."
-- Douglas Adams
Bookmarks