[英]skip specific rows and columns of csv with PHP
Hi I would like to know how can I skip certain colums/rows from a csv. 嗨,我想知道如何才能从csv中跳过某些列/行。 I have a form that uploads a cvs to MySQL and works fine, I also know how to skip the first line of the csv, but the format of the file I need to upload seems pretty hard for me. 我有一种将cvs上传到MySQL并可以正常工作的表格,我也知道如何跳过csv的第一行,但是我需要上传的文件格式对我来说似乎很难。 This is my code: 这是我的代码:
<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>
Thanks in advance. 提前致谢。 example of csv CSV的例子
Count the lines and skipp all lines before 14th one. 计算行数,并跳过第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>
Using your loop, here is a way to pull that off. 使用循环,这是实现此目标的一种方法。
USE ONE OF THESE NOT BOTH. 请勿同时使用其中之一。
$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]
AND SETUP YOUR LOOP 并设置你的圈
$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 SUGGESTIONS / HELPFUL BUT NOT NEEDED TO GET THIS WORKING 2建议/不需要此操作
Don't query in any for/while/each, its better to put the string together first then query once. 不要在任何时间/同时/每个中查询,最好先将字符串放在一起然后查询一次。
Don't use mysql, its old and insecure. 不要使用mysql,它既旧又不安全。 USE PDO or mysqli instead 改用PDO或mysqli
Your loop again 再次循环
$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."')";
}
take the query out of the loop, use implode to add the values in later 使查询脱离循环,稍后使用implode添加值
$query = "INSERT INTO databse.excel_table (name,email) VALUES (". implode("," , $values) .")";
$run = mysql_query($query);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.