/    Sign up×
Community /Pin to ProfileBookmark

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

[code=php]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(!$result) {
array_push($errArrUpdate, mysql_error());
} else {
//echo ‘Update succeeded’;
}
} else {
display_errors($errArrUpdate);
}
}[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@g0dzuki99May 24.2012 — [code=php]
//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;
[/code]


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.

[code=php]

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($errArrUpdate, mysql_error());
} else {
//echo 'Update succeeded';
}
} else {
display_errors($errArrUpdate);
} [/code]
Copy linkTweet thisAlerts:
@toptomatoauthorMay 24.2012 — thanks, i thought i'd tried break, but yes that works., thanks
×

Success!

Help @toptomato spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...