【问题标题】:How to display uploaded csv file into tablular format in drupal 7如何在drupal 7中将上传的csv文件显示为表格格式
【发布时间】:2017-07-08 13:17:50
【问题描述】:

我在 drupal 7 中创建了一个表单,它有一个上传文件的字段(仅限 csv 文件)现在如何在表单提交时将上传的 csv 文件显示到表格中?

【问题讨论】:

  • 什么表?你必须更好地解释这一点。
  • 我只想使用以下drupal内置函数并分配给一些变量以供进一步使用: $res = theme('table', array( 'header' => $header, 'rows' => $行));如何完成这项工作。提前致谢!

标签: forms drupal


【解决方案1】:

不确定主题功能,但你可以自己做。

即使用:

http://php.net/manual/en/function.file-get-contents.php

读取文件然后:

http://php.net/manual/en/function.str-getcsv.php

解析 CSV。

或者,也许:

http://php.net/manual/en/function.fgetcsv.php

逐行读取。无论如何,你最终会循环槽行,所以只需按照你想要的方式打印值,在值周围添加标记......

【讨论】:

  • 我已经创建了以下用于创建表的函数,但我只需要单独变量中的标题和行。函数测试($file_path, $header=false) {$handle = fopen($filename, "r"); echo '';if ($header) {$csvcontents = fgetcsv($handle);echo ''; foreach ($csvcontents as $headercolumn){echo"";}echo '';}while ($csvcontents = fgetcsv($handle)){echo '' ;foreach ($csvcontents as $column) {echo "";}echo '';}echo '
    $headercolumn
    $column
    ';fclose($handle);} 是吗可以将标题和行分开,以便我可以使用主题()?
  • 非常感谢@MilanG 的建议和指导。
【解决方案2】:

经过各种代码练习后,我想出了如下解决方案:

`function display_table($filename, $head=false) {
  $handle = fopen($filename, "r");
  $all_rows = array();
  $header = null;
  while ($row = fgetcsv($handle)) {
    if ($header === null) {
      $header = $row;
      continue;
    }
    $all_rows[] = array_combine($header, $row);
  }
  $table = theme('table', array('header' => $header, 'rows' => $all_rows));
  return $table;
}`

希望对其他人也有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2010-10-05
    • 2019-06-11
    • 2017-08-02
    相关资源
    最近更新 更多