0
我試圖根據唯一值構建div。因此,對於每一行,腳本都會根據$ tempTeam檢查團隊名稱。如果它們相等,則該行會被添加到現有div中的表格中。如果它們不相等,則創建一個新的div和表格,並將該行添加到那裏。變量定義在其他中被忽略如果
但是,if部分無法識別$ tempTeam是否等於團隊,所以它永遠不會運行,即使else部分正確設置$ tempTeam。所以我想知道爲什麼變量是爲代碼的其他部分工作,但不是if部分。
下面是完整的代碼,雖然麻煩從我第一次定義$ tempTeam開始。謝謝你的幫助。
<?php
$csv = file_get_contents('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 if ($tempTeam == '') {
$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>
";
}
else {
$tempTeam = $match['Team'];
echo "
</tbody>
</table>
</div>
<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>
";
?>
這就是問題。非常感謝您的支持。它的工作原理與現在一樣。 – Beau