使用PHP跳过csv的特定行和列

时间:2017-10-25 20:53:06

标签: php html mysql csv

您好我想知道如何跳过csv中的某些列/行。我有一个将cvs上传到MySQL并且工作正常的表单,我也知道如何跳过csv的第一行,但我需要上传的文件格式对我来说似乎很难。这是我的代码:

CREATE TABLE table_name (
    id  TEXT PRIMARY KEY,
    col TEXT,
    UNIQUE (id, col)
);

提前致谢。 example of csv

2 个答案:

答案 0 :(得分:0)

计算行数并在第14行之前跳过所有行。

    <body>

    <div class="container">
<?php
    if(isset($_POST['uploadBtn'])){
        $fileName=$_FILES['myFile']['name'];
        $fileTmpName=$_FILES['myFile']['tmp_name'];
        //FILE PATH
        $fileExtension=pathinfo($fileName,PATHINFO_EXTENSION);
        //ALLOWED FILE TYPES
        $allowedType = array('csv');
        if(!in_array($fileExtension,$allowedType)){?>

            <div class="alert alert-danger">
                INVALID FILE
            </div>
        <?php }else{

            $handle = fopen($fileTmpName, 'r');
            fgetcsv($handle);///////////////// SKIP FIRST ROW
            $k = 0;
            while (($myData = fgetcsv($handle,1000,','))!== FALSE){
             $k++;
              if ( $k > 14 ) {

                    $name = $myData[0];
                    $email = $myData[1];

                    $query = "INSERT INTO databse.excel_table (name,email)
                    VALUES ('".$name."','".$email."')";
                    $run = mysql_query($query);
                 }

            }
            if(!$run){
                die("error in uploading file".mysql_error());
            }else{ ?>
                    <div class="alert alert-success">
                        SUCCESS
                    </div>
        <?php   }
        }
    }
        ?>

    <form action="" method="post" enctype="multipart/form-data">
        <h3 class="text-center">
            RESULTS
        </h3></hr>
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <input type="file" name="myFile" class="form-control">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <input type="submit" name ="uploadBtn" class="btn btn-info">
                </div>
            </div>
        </div>
    </form>
    </div>
    </body>
    </html>

答案 1 :(得分:0)

使用你的循环,这是一种解决方法。

使用其中一个不是两个。

$rowsToSkip = [0,3,6,9]; //this is one way to manually enter row numbers
$rowsToSkip = range(5,10) //will return an array ex: [5,6,7,8,9,10]

设置你的循环

$i = 0; //this is used to keep an track of what row you are on
while (($myData = fgetcsv($handle,1000,','))!== FALSE){
  if($i == 0) continue; //this will skip your 1st row
  if(in_array($i, $rowsToSkip)) continue; //this will skip any row in $rowsToSkip array

  $name = $myData[0];
  $email = $myData[1];

  $query = "INSERT INTO databse.excel_table (name,email)
  VALUES ('".$name."','".$email."')";
  $run = mysql_query($query);
}

2建议/有帮助但不需要获得此工作

  • 不要在for / while / each中查询,最好先将字符串放在一起再查询一次。

  • 不要使用mysql,它老而且不安全。请改用PDO或mysqli

你的循环再次

$i = 0; //this is used to keep an track of what row you are on
$values = []; //will hold your row values
while (($myData = fgetcsv($handle,1000,','))!== FALSE){
  if($i == 0) continue; //this will skip your 1st row
  if(in_array($i, $rowsToSkip)) continue; //this will skip any row in $rowsToSkip array

  $name = $myData[0];
  $email = $myData[1];

  $values[] = "('".$name."','".$email."')";
}

将查询从循环中取出,使用implode在以后添加值

$query = "INSERT INTO databse.excel_table (name,email) VALUES (". implode("," , $values) .")";
$run = mysql_query($query);