answer counter table for php,How to exclude column header from CSV file in PHP?

这篇博客讨论了如何在使用PHP处理CSV文件时,跳过包含列头的第一行并将数据上传到MySQL数据库。作者提供了两种简单的方法:一种是读取文件并丢弃第一行,然后逐行插入数据;另一种是在插入语句中批量插入多个值,提高效率。同时,强调了在处理数据前进行数据清理的重要性。

When my client's are giving their inputs into the Excel file, they need the column header. But when they export the file into CSV (.csv) it includes the column heads into it. That's problem.

+---------+--------+-------------+

| Post id | Name | Designation |

+---------+--------+-------------+

101 noYon designer

102 nakib accountant

103 nishi developer

+---------+--------+-------------+

In my CSV (comma delimited) I got:

Post id,Name,Designation

101,noYon,designer

102,nakib,accountant

103,nishi,developer

I need to exclude the column heads (the first row) from the CSV when uploading it to MySQL using PHP.

Is there any way?

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

A simple solution: read the first line and discard it before reading the rest of the file line by line in a loop:

//get the csv file

$file = $_FILES[csv][tmp_name];

$handle = fopen($file,"r");

fgetcsv($handle); // Discard first line. If we know the maximum line length

// add it as second argument: it's slightly faster

while( $data = fgetcsv( $handle) ) {

$query = mysqli->query( "INSERT INTO table (post_id,name,designation) VALUES ('".

implode($data,"','")."'";

}

A faster solution which I've not coded here: append multiple sets of values to the INSERT like this:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

# Answer 2

With assistance of

//get the csv file

$file = $_FILES[csv][tmp_name];

$handle = fopen($file,"r"); // read the file

?>

// make the loop and do upload

$find_header = 0; // a simple counter

while( $data = fgetcsv( $handle,6000,",","'") ) {

$find_header++; //update counter

if( $find_header > 1 ) {

$sql = mysql_query( "INSERT INTO table_name (

post_id,

name,

designation

)

VALUES

(

'".$data[0] ."',

'".$data[1] ."'

'".$data[2]."'

)

");

}

} // endwhile

The logic is very simple and a bit childish. If we know how fgetcsv() works. fgetcsv() is:

The fgetcsv() function parses a line from an open file, checking for CSV fields.

The fgetcsv() function stops returning on a new line, at the specified length, or at EOF, whichever comes first.

That means it fetches each line of the CSV each time the loop rounds. So if we pass a simple PHP counter and exclude the first line, then it's that simple.

And of course, don't forget to sanitize the data using PHP sanitizing functions like trim() or addslashes() etc. :)

You can get my whole bunch of codes

# Answer 3

$filename = $_FILES["file"]["tmp_name"];

if ($_FILES["file"]["size"] > 0){

$file = fopen($filename, "r");

fgetcsv($file);

while (($getData = fgetcsv($file, 10000, ",")) !== false){

echo "

";

print_r($getData);

echo "

";

}

fclose($file);

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值