gpt4 book ai didi

php - PHP中的foreach错误处理功能

转载 作者:行者123 更新时间:2023-12-03 08:03:04 25 4
gpt4 key购买 nike

我有一个功能,可以对数据库中的数组执行foreach循环。

参见foreach ($teamarray AS $key => $value){$teamgo .= $value[1]." ".$value[2]."<br/>";
问题是,有时可能没有数据集,当循环命中该字段时会引发错误。

如何捕获/抑制此错误?

function GetUnsubmitted($coach){
$push .= "<div id=unsubmitted><h2>Check which event/teams you want to process and click submit</h2>";
$push .= "<form action=\"submit.php\" method=POST>";
$result = mysql_query("SELECT * FROM `ptable` WHERE coach = '$_SESSION[username]' AND status = '1' ORDER BY status ASC") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
$id = $row['id'];
$teampre = $row['team'];
$eventpre = $row['event'];
$statuspre = $row['status'];
$eventarray = DecodeEvent($eventpre);
$event = $eventarray[0];
$cat = $eventarray[1];
$subcat = $eventarray[2];
$division = $eventarray[3];
$type = $eventarray[4];

$teamarray = DecodeTeam($teampre);

$price = GetPrice($type, "nat");
$teamcount = count($teamarray);
$total = $price * $teamcount;

$teamgo = "";

foreach ($teamarray AS $key => $value){
$teamgo .= $value[1]." ".$value[2]."<br/>";

if($statuspre == "1"){
$statuscolor = "#FFCC99";
$statusmsg = "unsubmitted <a href=delsub.php?id=$id onClick=\"return confirm('Are you sure you want to delete this submission?');\"><img src=images/del.png border=0 />";
} elseif($statuspre == "2"){
$statuscolor = "#FFCC66";
$statusmsg = "awaiting confirmation";
} elseif($statuspre == "3"){
$statuscolor = "#66CC66";
$statusmsg = "confirmed";
}
}
$push .= "<div id=submission><div id=event style=\"background-color:$statuscolor;\"><h1>$event</h1><span id=status>$statusmsg</span></div><div id=subinfo><a href=\"#\" onClick=\"if($(this).next('div').css('display') == 'none') { $(this).next('div').show('fast'); } else { $(this).next('div').hide('fast'); } return false;\"><span id=dropdown><img src=images/expand.png border=0></span><h2>$cat >> $subcat >> $division >> $type</h2></a> <div id=team style=\"display:none;\">$teamgo<br />$price - $total<div id=controls></div></div></div></div>";
$pid .= $id;
$rtotal .= "$total,";
}


$stotal = explode(",", $rtotal);
$gtotal = array_sum($stotal);

$push .= "<div style=\"text-align:right;\"><div id=total>Total - <em>$gtotal</em></div><br><input type=image src=images/paynow.png alt=\"Pay Now\"></form> <a href=submit2.php?$pid&$pidarray><img src=images/mailfax.png width=138px height=41px border=0></a></div></div>";

return $push;
}

如果可能的话,id会说“未选择任何团队”,然后停止。

最佳答案

if ($array) {
foreach ($array as $k => $v) {
...
}
} else {
echo 'No team selected';
// exit from loop
}

您从循环退出将是“返回”或“break n”(n是要突破的水平)或继续……这取决于您的逻辑。

关于php - PHP中的foreach错误处理功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1699924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com