简体   繁体   中英

A variable won't display

I have code that runs a query on the database and returns some data and displays it. All very simple. I've tested the query and it work's perfectly.

So somewhere between the query being executed and me displaying the data, it's not working.

$q = "SELECT u.username, r.position, r.score, r.winner, t.team FROM ".TBL_FOOT_TOUR_ROUNDS." r
                      LEFT JOIN ".TBL_USERS." u ON u.id = r.winner
                      LEFT JOIN ".TBL_FOOT_TOUR_PLAYERS." pl ON pl.userid = r.winner
                      LEFT JOIN ".TBL_FOOT_TEAMS." t ON t.id = pl.team
                      WHERE r.tourid = '$tour_id' && r.round = '$i' ORDER BY r.position";
                $result = $database->query($q);
                ?>
                <div class="vertical-holder">
                    <div class="vertical-header"><p><?php echo $roundName[$i]; ?></p></div>
                    <?php
                    while($row=mysql_fetch_assoc($result)){
                        extract($row);
                        if($k&1){
                            ?>
                            <div class="horizontal-holder<?php echo $j; ?>">
                                <div class="white-holder">
                                    <div class="player-holder">
                                        <?php
                                        if($winner == 0){
                                            echo "<p>TBC</p>";
                                        }
                                        else{
                                            echo "<p><a href='/profile.php?id=$winner'>$username</a><br />$team</p>";
                                        }
                                        ?>
                                    </div>
                                    <div class="score-holder">
                                        <?php 
                                        if($score == NULL){
                                            echo "<p>-</p>";
                                        }
                                        else{
                                            echo "<p>$score</p>";
                                        }
                                        ?>
                                    </div>
                                </div>
                            <?php
                        }

That's the snippet of code that's (what I believe to be) relevant.

The score is showing as '-' all the time even when a score is present.

The rest of the returned data shows no problem.

Can anyone see why the score variable isn't showing?

Thanks

<?php 
    if($score == NULL){
        echo "<p>-</p>";
    } else {
        echo "<p>$score</p>";
    }
?>

you got it wrong. if the value in the database is null, $score will not be null, it will be the string "null". so try

<?php 
    if(strtoupper($score) === "NULL"){
        echo "<p>-</p>";
    } else {
        echo "<p>$score</p>";
    }
?>

:)

alternatively you can create some utility function that changes a variable:

function nullify(&$data) {
    if(strtoupper($data) === "NULL") {
        $data = NULL;
    }
}

and then call it like this:

nullify($score);

if $score should be set to null, it will be null after the call. then you can keep your logic the way it is ^^

Not sure what $score contains, but it seems to me that you could write your test like:

if($score < -1){ // or $score <= -1
    echo "<p>-</p>";
}
else{
    echo "<p>$score</p>";
}

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