简体   繁体   中英

Printing something once in a while loop

I have this bit of code within a while loop.

$hometeamwin=(lose,win) so with that bit of code above it is printing "winlose"

If there's occurrences of 'lose' within $hometeamwin I'd like it to print "lose" (but only once!)

if there's no occurrences of 'lose' print 'win' (but only once!)

If I understand your problem (I'm not positive I do) you want to look at all of the bets and if any of them resulted in a loss, then after you list them all, you want to display win or loss depending on if there was a loss among the results. I copied your code and added 3 lines marked with #ADDED. Add those three lines to acheive what I described.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM