Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-12-2005, 11:37 AM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
read tab delimited file

was just helping a friend with some code. i was gonna just use this as a temporary post, but it might help someone in the future too, so i'll keep it here.

this code will read a tab delimited file and loop through each line. you can change the delimited /t to whatever ou want. default is a comma.

PHP Code:
<?
// mysql connection script ...

// tab delimited file
$file "test_questions.csv";

// open file
$handle fopen($file"r");

// loop through results with fgetcsv() function
while(($data fgetcsv($handle1000"\t")) !== FALSE) {
    
    
// populate field vars just to make it easier to work with .. 
    // you could access the $data[] array directly in the sql if you want
    
$field1 $data[0];
    
$field2 $data[1];
    
$field3 $data[2];
    
$field4 $data[2];
    
// etc ...
    
    // build your sql statement
    
$sql "insert into table set 
                                testid='1',
                                category='foo',
                                field1='"
.$field1."',
                                field2='"
.$field2."',
                                field3='"
.$field3."',
                                field4='"
.$field4."'";
    
    
$result mysql_query($sql);
}

// close file
fclose($handle);
?>
__________________
Mike
sde is offline   Reply With Quote
Old 09-13-2005, 07:49 PM   #2 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Handy... And more scalable than explode() if you have a huge file. (first time I've seen fgetcsv -- then again I suppose this function has been around since php3 .. guess once I get it to read the data from a line I do the rest by hand.)

-r
idx is offline   Reply With Quote
Old 09-13-2005, 10:18 PM   #3 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
I dig this snippet. Though it does make me yearn for perl's split in php... Great post, Mike.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
File Read Error Messages Salchester Standard C, C++ 4 09-04-2005 09:28 AM
getting java to read from a file. philthee Java 6 10-27-2004 09:00 AM


All times are GMT -8. The time now is 02:20 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting