PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 18th, 2004, 08:12 PM
usagipun usagipun is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 usagipun User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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;
}
?>

Reply With Quote
  #2  
Old July 19th, 2004, 10:17 AM
dtalukdar dtalukdar is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Location: KS, USA
Posts: 3 dtalukdar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to dtalukdar
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!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > skip first line in csv file when uploading


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT