簡體   English   中英

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

[英]skip specific rows and columns of csv with PHP

嗨,我想知道如何才能從csv中跳過某些列/行。 我有一種將cvs上傳到MySQL並可以正常工作的表格,我也知道如何跳過csv的第一行,但是我需要上傳的文件格式對我來說似乎很難。 這是我的代碼:

<body>

<div class="container">

[enter image description here][1]<?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
        while (($myData = fgetcsv($handle,1000,','))!== FALSE){
                $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>

提前致謝。 CSV的例子

計算行數,並跳過第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>

使用循環,這是實現此目標的一種方法。

請勿同時使用其中之一。

$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建議/不需要此操作

  • 不要在任何時間/同時/每個中查詢,最好先將字符串放在一起然后查詢一次。

  • 不要使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM