|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
skip first line in csv file when uploading
Hi Im new here. I wrote a script in php to upload csv file through web-based form into phmyadmin.
My .csv file is like that: =========================== id country_name country_code 1 JPN 81 2 VNE 84 .... MY script worked well, but the problem is that I want to skip the header line (which including id, country_name, country_code) in .csv file. How can i do that? Any helps will be highly appriciated. Here is my code: Code:
<?php
/*
set table: id, country_name, country_code
*/
// connect to DB
$host_name = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "country";
$table = 'country';
switch ($_POST['act']) {
case 'insert':
$db = mysql_connect("$host_name","$db_user","$db_password") or die("Could not connect to database!");
$result = mysql_select_db($db_name) or die("Could not select database!");
// open file
$fp = fopen($_FILES['userfile']['tmp_name'], 'r') or die('Could not open file!');
while ($data = fgetcsv($fp, 1024)) {
$sql_ = "SELECT id FROM $table WHERE id=$data[0] ";
$result_=mysql_query($sql_, $db) or die("Could not select id from $table!");
$id_=mysql_fetch_row($result_);
if($id_[0]!="") {
// update record in case id was already existed/
$update = "UPDATE $table SET country_name='$data[1]', country_code='$data[2]' WHERE id=$data[0] ";
$res_update = mysql_query($update, $db) or die("Could not update DB!");
echo "Row #".$num." was updated!<p>";
$num++;
}
else
{
// insert new record into database
$insert = "INSERT INTO $table (id, country_name, country_code) VALUES( '$data[0]', '$data[1]' , '$data[2]') ";
$res_insert = mysql_query($insert, $db) or die("Could not INSERT query!");
echo "Row #".$num." was inserted!<p>";
$num++;
}
}
fclose($fp);
break;
default:
echo <<<EOD
<center>
<h3>CSV file upload form</h3>
<form enctype="multipart/form-data" method="POST">
<input type="hidden" name="act" value="insert">
CSV file <input name="userfile" type="file">
<input type="submit" value="Submit">
</form>
</center>
EOD;
break;
}
?>
|
|
#2
|
|||
|
|||
|
Looks like you are using the fgetcsv function.
I have a slightly different way of handling this. I would load my file in as Code:
$fp = file($_FILES['userfile']['tmp_name']); to begin with. This sets up $fp as an array of lines, which still remain in CSV format. The next step would be to remove the first line, hence: Code:
array_shift($fp); Now, you have your file full of CSV values, sans the header The third stage is to extract your data.... something to the tune of the following: Code:
echo "<table>";
for ($x=0; $x<=sizeof($fp); $x++)
{
$values=split(",", $fp[$x]);
echo "<tr>";
for ($y=0; $y<=sizeof($values); $y++)
{
echo "<td>$values[$y]</td>
}
echo "</tr>";
}
echo "</table>";
Hope this helps! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > skip first line in csv file when uploading |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|