dongluan5740 2014-04-16 18:57
浏览 69

构建转换CSV文件的应用程序

I have a rough and complete working CSV transformer. The way my current system works is it imports the CSV file into an SQL database table with static column names, and exports only specific (needed) columns. This system works great but is only specific to one type of CSV file (because the column names are pre-defined.) I'm wondering how I can make this universal. Instead of having it insert column1, column2, column3. I want to insert Spreadsheet Column1, Spreadsheet Column2, Spreadsheet Column3, etc. How would I go about pulling the column names from the CSV file, and creating a new table in the database with the column names being those from the first row of the CSV file.

The current system:

  • Client uploads CSV file.
  • A table is created with predefined column names (column 1, column 2, column 3)
  • Using LOAD DATA INFILE -> PHP scripts will insert the information from the CSV file into the recently created table.
  • The next query that is ran is simply something along the lines of taking only specific columns out of the table and exporting it to a final CSV file.

The system that would be ideal:

  • Client uploads CSV file.
  • PHP scripts read the CSV file and takes only the first row (column names), after taking these column names, it'll create a new table based on the column names.
  • PHP scripts now use LOAD DATA INFILE.
  • The rest is the same as current system.

Current code:

import.php

include("/inc/database.php");
include("/inc/functions.php"); 
include("/inc/data.php"); 
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$string = random_string(7);
$new_file_name = 'report_'. $string .'.csv';
$themove = move_uploaded_file($_FILES['csv']['tmp_name'], 'C:/xampp/htdocs/uploads/'.$new_file_name);
        mysql_query("CREATE TABLE report_". $string ."(". $colNames .")") or die(mysql_error());
    $sql = "LOAD DATA INFILE '/xampp/htdocs/uploads/report_". $string .".csv'
INTO TABLE report_". $string ."
FIELDS TERMINATED BY ','
LINES TERMINATED BY '
'
(". $insertColNames .")";
    $query = mysql_query($sql) or die(mysql_error());
    header('Location: download.php?dlname='.$string.'');
}

data.php (shortened most of this. In reality there are about 200 columns going in, twenty-thirty coming out)

<?php
$colNames = "Web_Site_Member_ID text, 
Master_Member_ID text, 
API_GUID text, 
Constituent_ID text";
$insertColNames = "Web_Site_Member_ID,
Master_Member_ID,
API_GUID,
Constituent_ID";
$exportNames = "Web_Site_Member_ID, Date_Membership_Expires, Membership, Member_Type_Code";
?>

functions.php just includes the block of code for generating a random string/file name.

  • 写回答

1条回答 默认 最新

  • dporb84480 2014-04-16 19:18
    关注

    For CSV file reading please look at the fgetcsv() function. You should easily be able to extract a row of data and access each individual field in the resulting array for your column header definitions.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭