php foreach error handling in function

2019-08-30 07:08发布

I have a function which performs a foreach loop on an array from a database.

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

Problem is, sometimes there may be no data set, which throws an error when the loop hits that field.

How can i catch/suppress this error?

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;
}

If possible id like it to say "no team selected" and stop.

6条回答
爷的心禁止访问
2楼-- · 2019-08-30 07:37
if ($array) {
    foreach ($array as $k => $v) {
        ...
    }
} else {
    echo 'No team selected';
    // exit from loop
}

Your exit from loop will be a "return", or a "break n" (n is the levels to break for) or continue... it depends on your logic.

查看更多
\"骚年 ilove
3楼-- · 2019-08-30 07:41

<insert puzzled smiley here>

foreach($row AS $key => $value) { 
    if ($value) {
        $row[$key] = stripslashes($value);
    } 
} 

And:

foreach ($teamarray AS $key => $value){
    if ($value && sizeof($value) > 2) {
        $teamgo .= $value[1] . $value[2]
    }
}

Is this it?

查看更多
老娘就宠你
4楼-- · 2019-08-30 07:42

You can write so:

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

foreach expects array. So the really correct way is to ensure that you deal with array before try to iterate, like this:

if (is_array($teamarray) && count($teamarray)) {
    foreach ((array)$teamarray as $key => $value) {
        $teamgo .= $value[1] . " " . $value[2] . "<br/>";
        //...
    }
}

You also can check is_iterable since PHP 7.1.

查看更多
放荡不羁爱自由
5楼-- · 2019-08-30 07:50

Just do a test if $teamarray actually is an array:

if (is_array($teamarray)) {
    foreach ($teamarray as $key => $value) {
        // …
    }
}
查看更多
疯言疯语
6楼-- · 2019-08-30 08:01

Or you could do:

$teamarray = isset($teamarray) ? $teamarray : array();

Just prior to the loop in a nice tidy line, and it would ensure that you have the variable set to an empty array which would cause it to skip the foreach().

查看更多
三岁会撩人
7楼-- · 2019-08-30 08:03
if ($value !== null && count($value) >= 3) {


$teamgo .= $value[1] . $value[2]
}
查看更多
登录 后发表回答