duanjue9889 2016-05-26 06:10
浏览 17
已采纳

在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('schedules.csv');

$csv_array = explode("
", $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>
";          
?>
  • 写回答

1条回答 默认 最新

  • dongshan4549 2016-05-26 06:13
    关注

    For a start, look at the first line of the else clause:

    $tempTeam == $match['Team'];
    

    I'm pretty certain that's not what you wanted to do, it seems to me that assignment would probably be a better choice than comparison.

    What you're doing is no different to:

    $a = 1;
    $a == $a + 1;
    print ($a);
    

    which will still output 1 rather than 2. If you want do do assignment, it should have just the one = character:

    $tempTeam = $match['Team'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?