sark9012
sark9012

Reputation: 5727

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

Upvotes: 0

Views: 138

Answers (2)

davogotland
davogotland

Reputation: 2777

<?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 ^^

Upvotes: 3

Toto
Toto

Reputation: 91385

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

Upvotes: 2

Related Questions