skip to Main Content

I have two meta fields in WordPress (ACF) that track the score of our soccer games. It’s a group field with a name of ‘score’ with two number fields in it, ‘bucs’ (that’s us, the Bucs) and ‘opp’ for our opponent.

I am able to assign the variables and display them easy enough with:

$score = get_field('score');
    $bucsscore = $score['bucs'];
    $oppscore  = $score['opp'];

echo $bucsscore;

echo $oppscore;

Now my goal is to assign a class to a div based on the game outcome: win, lose, tie. I am trying to get this class by simply comparing the two scores.

What I have so far gives me inconsistent results on every game. Sometimes a 0 – 0 score says we lost. If we win 1 – 0 it says we tie. If we tie 1 – 1 it says we won.

<?php if($bucscore === $oppscore) { echo 'tie-game'; } elseif ($bucscore < $oppscore) { echo 'lost-game'; } elseif ($bucscore > $oppscore) { echo 'won-game'; }?>

I have tested the ‘===’ with ‘=’ and ‘==’ and still get the same inconsistency. I am thinking I can reduce the amount of code if I could create a variable that gave me the class based on the comparison but I am stuck here for now.

2

Answers


  1. This code works fine for me when just assigning numbers to the vars. Maybe there’s something wonky with how the data is coming from ACF?

    Try a var_dump to see what actual datatypes you’re getting from ACF:

    echo var_dump($bucscore);

    echo var_dump($oppscore);

    Login or Signup to reply.
  2. In your code, it seems there’s a typo in the variable name $bucscore. It should be $bucsscore.

    if ($bucsscore === $oppscore) {
        echo 'tie-game';
    } elseif ($bucsscore < $oppscore) {
        echo 'lost-game';
    } elseif ($bucsscore > $oppscore) {
        echo 'won-game';
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search