【问题标题】:Print first 3 columns of CSV file in table format in PHP upon file upload上传文件时以 PHP 表格格式打印 CSV 文件的前 3 列
【发布时间】:2012-07-16 16:19:45
【问题描述】:

我是 PHP 新手,遇到了这个问题。

$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
if (((in_array($_FILES['uploadedfile']['type'],$mimes)))
    && ($_FILES["uploadedfile"]["size"] > 0))
{
    $filename = $_SESSION['username'].$_FILES["uploadedfile"]["name"];
    move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],"./uploads/".$filename);

    echo "Stored in: " . "./uploads/" .$filename;
    echo "<br />\n";
    echo 'Daylight hours will be calculated for this latitude : '.$_POST['latCSV'];
    echo "<br>";
    //print out uploaded file.csv
    echo "<table BORDER=1>\n\n";
    echo "  <tr><th>AvgCrown</th><th>MinCrown</th><th>MaxCrown</th><th>dateLogged</th><th>DaylightHours</th></tr>";
    echo "<tbody id='tblBdyLoggedData'>";
    $f = fopen("./uploads/" .$filename, "r");
    $i=0;   //just a counter
    while (($line = fgetcsv($f)) !== false) {
        echo "<tr>";
        foreach ($line as $cell) {
            // Save the value before you output
            $lastColValue = htmlspecialchars($cell);
            //unset($lastColValue[3]);
            echo "<td>" . $lastColValue . "</td>";

        }
        //checking if date is valid
        checkDateString($lastColValue);

        // If you want to store all log dates in an array:
        $logDates[] = $lastColValue;                            
        $firstlogDate = $logDates[$i];
        echo "<td>" . checkDateString($lastColValue) . "</td>";
        //call function using firstlogDate for every row
        echo "<td>" . calcDaylight($firstlogDate,$_POST["latCSV"]) . "            </td>";
        echo "</tr>\n";
        $i = $i+1;
    }
    fclose($f);
    echo "</tbody></table></body></html>";
}

我想在文件上传时仅打印 CSV 文件的前 3 列,因为其他两列将使用各自的函数进行计算。 Datelogged 将从 csv 中获取并正确重新格式化,并且还会根据 datelogged 计算日光时间。

【问题讨论】:

    标签: php file-upload html-table import-from-csv


    【解决方案1】:

    首先,使用 PHP 文件系统库解析 $file_contents:

    $foo = split(',', $file_contents);

    参考http://php.net/manual/en/book.filesystem.php

    现在您可以访问前三列:

    echo foo[0] . '/' . foo[1] . '/' . foo[2];
    

    【讨论】:

      【解决方案2】:

      希望这段代码对你有帮助

      ?php
      echo "<html><body><table>\n\n";
      $f = fopen("so-csv.csv", "r");
      while (($line = fgetcsv($f)) !== false) {
              $count=0;
              echo "<tr>";
              foreach ($line as $cell ) 
              {
                  if($count<3)
                  {    
                      echo "<td>" . htmlspecialchars($cell) . "</td>";
                      $count++;
                  }
              }
              echo "<tr>\n";
      }
      fclose($f);
      echo "\n</table></body></html>";
      

      【讨论】:

      • @Hardik...嗯,它确实有帮助,但不是给我前 3 列,而是给我前 3 行。但是感谢您的帮助!
      • 我更新了我的代码,将前三行作为表格行的三列提供给您
      • AvgCrown, MinCrown, MaxCrown, dateLogged 我想传递所有这些值,但我只想显示表中的前 3 列..
      • 我已经更新了代码。如果有任何问题,请试试这个然后告诉我。我还没有测试过这段代码,所以它可能有一些错误。
      • 你是不是又改了代码。。现在好像坏了。。当你有 if 语句时它工作了
      【解决方案3】:

      替换这个:

                          foreach ($line as $cell) {
                              // Save the value before you output
                              $lastColValue = htmlspecialchars($cell);
                              //unset($lastColValue[3]);
                              echo "<td>" . $lastColValue . "</td>";
      
                          }
      

      用这个:

                          $lineValues = array_values($line);
                          if (count($line) < 3) {
                              echo "<td>This line is unusable</td><td></td><td></td>";
                          }
                          for ($colNum = 0; $colNum < 3; $colNum++) {
                              echo "<td>" . htmlspecialchars($lineValues[$colNum]) . "</td>";
                          }
      

      【讨论】:

        猜你喜欢
        • 2021-05-19
        • 2019-12-20
        • 2020-01-24
        • 2022-01-04
        • 2020-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多