偶尔打印一次

时间:2012-12-17 22:44:24

标签: php if-statement while-loop

我在while循环中有这段代码。

$ hometeamwin =(输,赢)所以上面那段代码是打印" winlose"

如果出现“失败”的情况。在$ hometeamwin中我喜欢打印"输掉" (但只有一次!)

如果没有出现“失败”的情况。打印'赢得' (但只有一次!)

1 个答案:

答案 0 :(得分:2)

如果我理解你的问题(我不是肯定的话)你想查看所有的赌注,如果其中任何一个导致了损失,那么在你全部列出之后,你想显示输赢取决于结果中是否有损失。我复制了你的代码并添加了3行标有#ADDED的行。添加这三行来实现我所描述的内容。

if($num_rows) {
    $wonBet = true;  #ADDED
    while ($row = mysql_fetch_assoc($result)) {
        echo "<tr>";
        if(!$row["ht_score"]) {
            $halftime = "-";
        } else {
            $halftime = $row["ht_score"];
        }
        if(!$row["ft_score"]) {
            $fulltime = "-";
        } else {
            $fulltime = $row["ft_score"];
        }

        echo "<td>" . $row["home_team"] . "</td>" . "<td>" . " vs " . "</td>" . "<td>" . $row["away_team"] . "</td>" . "<td>" . $halftime . "</td>" . "<td>" . $fulltime . "</td>";

        $value = $fulltime;
        $apart = explode('-',$value); 
        if($apart[0] > $apart[1] && $row["home_id"]==$row["winningid"]) {
            $hometeamwin = "Win";
        } else if($apart[0] < $apart[1] && $row["away_id"]==$row["winningid"]) {
            $hometeamwin = "Win"; 
        } else if($apart[0] == $apart[1] && !$row["winningid"]) {
            $hometeamwin = "Win"; 
        } else { 
            $hometeamwin = "lose";    
        }

        if($fulltime!=$row["ft_score"]) {
             $hometeamwin = "";
        }
        if($hometeamwin == "lose"){  #ADDED
            $wonBet = false;         #ADDED
        }                            #ADDED
        echo "<td>" . $hometeamwin . "</td>";
        echo "</tr>";
    }

    echo ($wonBet) ? "You won bet" : "You lost bet";  #ADDED
}