将CSV列数据放入Div类

时间:2016-05-22 18:46:09

标签: php arrays

我有这个PHP脚本,可以用csv数据创建多个表。它工作得很好,但我不确定如何将第一列(League)放入div类中,以便我可以将每个列附加到匹配的父div和jquery中。

<?php
$csv = "League,Team,Date,Opponent,Result
american,MIN,May 3,UTA,W
american,SAC,May 3,DAL,L
american,SAC,May 4,TOR,W
american,SAC,May 7,MIL,W
national,NYN,May 5,BAL,L
national,NYN,May 7,MIA,W
national,DET,May 6,DEN,L
national,DET,May 7,POR,L";

$csv_array = explode("\n", $csv);

$tables = [];
foreach($csv_array as $key => $value) {
    if ($key == 0) {
        continue;
    }
    $line = explode(',', $value);
    if (array_key_exists($line[1], $tables)) {
        $tables[$line[1]][] = $line;
    } else {
        $tables[$line[1]] = [$line];
    }
}

foreach ($tables as $key => $value) {
    echo "<div class=\"$line[0]\">";
    echo "<table><tr><th>$key</th></tr><tr>";
    foreach (explode(',', $csv_array[0]) as $keyHeader => $valueHeader) {
                if (in_array($keyHeader, [0, 1])) {
            continue;
        }
        echo "<th>$valueHeader</th>";
    }
    echo '</tr>';
    foreach ($value as $keyRow => $valueRow) {
        echo '<tr>';
            foreach ($valueRow as $keyValue => $valueValue) {
                if (in_array($keyValue, [0, 1])) {
                    continue;
                }
                echo "<td>$valueValue</td>";
            }
        echo '</tr>';
    }
    echo '</table></div>';
}
?>

我知道数据在$ line [0]中,但是我可以理解这一点。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

很难真实地告诉我们您的代码是如何逐行进行的,所以您可能会考虑更好地评估每个步骤以记录您尝试的内容实现。在以后尝试查看您自己的代码时,这将最有利于您。

我无法根据您构建迭代器的方式真正看到提取相应联盟的直接方法。 $line[0]总是&#34;国家&#34;是因为这是你在迭代列表时分配给它的最后一个值:

foreach($csv_array as $key => $value) {
    if ($key == 0) {
        continue;
    }
    $line = explode(',', $value);

我确信它可以减少重新设计,但就像我说的那样,我对每行代码的目的都不太清楚。特别是,如果您有错误报告,那么您会注意到很多:

  

注意:未定义的偏移量:

中的1
if (array_key_exists($line[1], $tables)) {
    $tables[$line[1]][] = $line;
} else {
    $tables[$line[1]] = [$line];
}

顺便说一句,如果您对生成CSV输出有更多的控制权,比如关系数据库,那么这可能会容易得多,那么您可以使用GROUP BY子句并根据您的特定需求获得更好的输出格式。

无论如何,为了解决这个问题,我首先通过填充所有CSV值的解析数组来选择完全重新设计。

<?php
// no need for extra \n's when inside quoted string
$csv = "League,Team,Date,Opponent,Result
american,MIN,May 3,UTA,W
american,MIN,May 4,SEA,L
american,SAC,May 3,DAL,L
american,SAC,May 4,TOR,W
national,NYN,May 5,BAL,L
national,NYN,May 7,MIA,W
national,DET,May 6,DEN,L
national,DET,May 7,POR,L";

$csv_array = explode("\n", $csv);

$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];
  }

}

这将为您提供一个整洁的关联数组,如下所示:

Array
(
    [1] => Array
        (
            [League] => american
            [Team] => MIN
            [Date] => May 3
            [Opponent] => UTA
            [Result] => W
        )

    [2] => Array
        (
            [League] => american
            [Team] => MIN
            [Date] => May 4
            [Opponent] => SEA
            [Result] => L
        )

    [3] => Array
        (
            [League] => american
            [Team] => SAC
            [Date] => May 3
            [Opponent] => DAL
            [Result] => L
        )

    [4] => Array
        (
            [League] => american
            [Team] => SAC
            [Date] => May 4
            [Opponent] => TOR
            [Result] => W
        )

    [5] => Array
        (
            [League] => national
            [Team] => NYN
            [Date] => May 5
            [Opponent] => BAL
            [Result] => L
        )

    [6] => Array
        (
            [League] => national
            [Team] => NYN
            [Date] => May 7
            [Opponent] => MIA
            [Result] => W
        )

    [7] => Array
        (
            [League] => national
            [Team] => DET
            [Date] => May 6
            [Opponent] => DEN
            [Result] => L
        )

    [8] => Array
        (
            [League] => national
            [Team] => DET
            [Date] => May 7
            [Opponent] => POR
            [Result] => L
        )

)

现在,从这些数据生成自定义div和表是一个相当简单的过程:

// create divs and tables
foreach ($parsed_array as $i => $game) {

  // since we're interested in pairs, then create new div every other one
  // if odd, then open div/table
  if ($i % 2 !== 0){
    echo "
      <div class='$game[League]'>
        <table>
          <tr>
            <th>$game[Team]</th>
          </tr>
          <tr>
            <th>Date</th><th>Opponent</th><th>Result</th>
          </tr>
    ";
  }

  // print data
  echo "
          <tr><td>$game[Date]</td><td>$game[Opponent]</td><td>$game[Result]</td></tr>
  ";

  // if even, then close div/table
  if ($i % 2 === 0){
    echo "
        </table>
      </div>
    ";
  }

}

这将生成您问题中提供的html,其中正确的联赛为div类名称:

&#13;
&#13;
<div class='american'>
  <table>
    <tr>
      <th>MIN</th>
    </tr>
    <tr>
      <th>Date</th><th>Opponent</th><th>Result</th>
    </tr>

    <tr><td>May 3</td><td>UTA</td><td>W</td></tr>

    <tr><td>May 4</td><td>SEA</td><td>L</td></tr>

  </table>
</div>

<div class='american'>
  <table>
    <tr>
      <th>SAC</th>
    </tr>
    <tr>
      <th>Date</th><th>Opponent</th><th>Result</th>
    </tr>

    <tr><td>May 3</td><td>DAL</td><td>L</td></tr>

    <tr><td>May 4</td><td>TOR</td><td>W</td></tr>

  </table>
</div>

<div class='national'>
  <table>
    <tr>
      <th>NYN</th>
    </tr>
    <tr>
      <th>Date</th><th>Opponent</th><th>Result</th>
    </tr>

    <tr><td>May 5</td><td>BAL</td><td>L</td></tr>

    <tr><td>May 7</td><td>MIA</td><td>W</td></tr>

  </table>
</div>

<div class='national'>
  <table>
    <tr>
      <th>DET</th>
    </tr>
    <tr>
      <th>Date</th><th>Opponent</th><th>Result</th>
    </tr>

    <tr><td>May 6</td><td>DEN</td><td>L</td></tr>

    <tr><td>May 7</td><td>POR</td><td>L</td></tr>

  </table>
</div>
&#13;
&#13;
&#13;