mercredi 25 mai 2016

Variable Defined in Else Is Ignored in If

I'm trying to build divs based on unique values. So for each line, the script checks the team name against $tempTeam. If they're equal, the line gets added to the table in the existing div. If they're not equal, a new div and table is created, and the line is added there.

However, the if part isn't recognizing when $tempTeam is equal to the team so it never runs, even though the else part is properly setting $tempTeam. So I'm wondering why the variable is working for the else part of the code, but not the if part.

Here's the full code, although the trouble begins when I first define $tempTeam. Thanks for any help.

<?php
$csv = file_get_contents('assets/schedules.csv');

$csv_array = explode("\n", $csv);
unset($csv_array[count($csv_array) - 1]);
$headers = explode(",", $csv_array[0]);

// iterate through all lines of CSV, skipping first header line
for($i=1;$i<count($csv_array);$i++) {

  $line = explode(",", $csv_array[$i]);

  // iterate through all headers and assign corresponding line value
  foreach ($headers as $index => $header){
    $parsed_array[$i][$header] = $line[$index];
  }

}

// create divs and tables
$tempTeam = '';
foreach ($parsed_array as $i => $match) {

    if ($tempTeam == $match['Team']) {
        echo "
            <tr><td>$match[Date]</td><td>$match[Result]</td><td>$match[Location]</td><td>$match[Opponent]</td></tr>
        ";
    }

  else {
        $tempTeam == $match['Team'];
    echo "
            <div class=\"schedule\" data-container=\"$match[League]\">
                <table>
                    <thead>
                        <tr>
                            <th colspan=\"4\">$match[Team]</th>
            </tr>
            <tr>
                <th>Date</th><th>Result</th><th>Location</th><th>Opponent</th>
            </tr>
                    </thead>
                    <tbody>
                        <tr><td>$match[Date]</td><td>$match[Result]</td><td>$match[Location]</td><td>$match[Opponent]</td></tr>
        ";
    }

    echo "
                </tbody>
            </table>
        </div>
    ";

}
?>

Aucun commentaire:

Enregistrer un commentaire