简体   繁体   English

从csv文件中读取第一行并根据它创建表(csv文件字段)

[英]Read first row from csv file and create table according to it(csv file fields)

Read first row from csv file and create table automatically according to it(csv file fields) in mysql. 从csv文件中读取第一行并根据它在mysql中自动创建表(csv文件字段)。 Looking for PHP script? 寻找PHP脚本? I have tried this 我试过这个

 <?php

$arr = array(array(),array());
$num = 0;
$row = 0;
$handle = fopen("./contacts.csv", "r");


while($data = fgetcsv($handle,1000,",")){   
    $num = count($data);
    for ($c=0; $c < $num; $c++) {
            $arr[$row][$c] = $data[$c];
    }
    $row++;
}


$con = mysql_connect('localhost','root','');
mysql_select_db("excel_database",$con);

for($i=1; $i<$row; $i++){
$sql = "INSERT INTO contacts VALUES ('".$arr[$i][0]."','".$arr[$i][1]."','".$arr[$i][2]."','".$arr[$i][3]."','".$arr[$i][4]."','".$arr[$i][5]."')";
mysql_query($sql,$con);
}

?>

Modifiying your script a bit, this should do the trick and create the table according to the csv header line: 稍微修改一下你的脚本,这应该可以解决问题并根据csv标题行创建表:

    $sql[] = array();
    $handle = fopen($file, "r");

    $header = fgetcsv($handle,1000,",");
    if($header){
        $header_sql = array();
        foreach($header as $h){
            $header_sql[] = '`'.$h.'` VARCHAR(255)';
        }
        $sql[] = 'CREATE TABLE `imported_csv` ('.implode(',',$header_sql).')';

        while($data = fgetcsv($handle,1000,",")){   
            $sql[] = "INSERT INTO imported_csv VALUES ('".implode("','",$data)."')";
        }
    }        

    $con = mysql_connect('localhost','root','');
    mysql_select_db("excel_database",$con);
    foreach($sql as $s){
        mysql_query($s,$con);
    }

Here's a single function to give you an array: fgetcsv See Example #1 on that page for a basic script. 这是一个为您提供数组的函数fgetcsv请参阅该页面上的示例#1以获取基本脚本。

The table will just be a matter of looping over the contents ... 该表只是循环内容的问题......

Aha, I finally parsed "table automatically according to it in mysql" and understood. 啊哈,我终于解析了“在mysql中根据它自动表”并理解了。 Sorry. 抱歉。 fgetcsv will return an array; fgetcsv将返回一个数组; just use that array to create your sql command. 只需使用该数组来创建您的sql命令。 eg '...values (' . implode(',', $results_of_fgetcsv) . ')...' 例如'...values (' . implode(',', $results_of_fgetcsv) . ')...'

This is half way to your question. 这是你问题的一半。

<?php
    $row = 1;
    if (($handle = fopen("ab.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            echo "<p> $num fields in line $row: <br /></p>\n";
            $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";

            }
        }

        fclose($handle);
    }
    ?>

If you want to visualize the table data, in html, you can create a table using PHP and data from csv reading the first line as headers. 如果要在html中可视化表数据,可以使用PHP创建表,并将csv中的数据作为标题读取第一行。 I use this function that you can try it! 我用这个功能你可以尝试一下!

function build_table($source){
$data = array();
$file = fopen($source, 'r');
while (($result = fgetcsv($file,0,";")) !== false){$data[] = $result;}
fclose($file);


//Now process the data and create table
$data_rows=@count($data[0]); //from the first row get the header name
$colum_trail="";
//Create the header part
for($i=0; $i<$data_rows;$i++){
$cell=$data[0][$i]; //each header
$colum_trail.="<th>$cell</th>";    
}

////Create the table body contents     
$table_cells="";

for($i=1; $i<count($data);$i++){
$table_cells.="<tr>";       
$cell=$data[$i];

foreach($cell as $cell_value){
$cell_v=$cell_value;
$table_cells.="<td>$cell_v</td>";
}
$table_cells.="</tr>";
}

$table="<table><tr>$colum_trail</tr>$table_cells</table>";
echo "<style>td {border-bottom: 2px solid black;}</style>";
echo $table;
}

Then all you need is to after that code, call that function like this: 然后您需要的是在该代码之后,调用该函数:

build_table("source.csv");  //then call the file you want to create table

array_shift(file('filename.csv'))将为您提供文档filename.csv的第一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM