make a PHP script to Import files ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

make a PHP script to Import files ?

Post by jramaro »

Hi,
Im tyring to come up with a solution for this

Say I have an address list like this:


Mr Tom Smith,
555 Somethin Sea Dr,
CA,
91207,
(222) 555-55555

with over 5,000 data blocks like that.
and if i want to have PHP import this data to database.
in database fields as:

record_id | name | address | city | state | zip_code | phone

What would be the best way ?
is it possible to break apart that data into seperate variables from a list with comma seperated strings like that ?


Thank You
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fgetcsv() if the data is provided to you may be of use here.
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

I think i found a good way to do this.
But have a question on the next step

Here's the part thats working

Code: Select all

$data = " Tom Smith, 
                 Somethin Sea Dr,
                 Someplace City,
                 CA, 91226, 
                 (222) 555-55555,
               ";

list($name, $address, $city, $state, $zip_code, $phone) = explode(",", $data);

echo $name . "<BR>";
echo $address . "<BR>"; 
echo $city . "<BR>";
echo $state . "<BR>";
echo $zip_code . "<BR>";
echo $phone . "<BR>";




but since my $data variable will have more than one record block , how can i echo each of these out seperately?

I've used while loops in displaying multiple database records *from* a database. but not sure how with this , since theres
going to be no while($row=mysql etc.


Thank You
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Well, your best bet for importing files would be to grab the data line by line and when you have a full record, then process it and move on.

Your biggest apparent issue is that there needs to either be exactly 5 lines of data per record, or a blank line between records. Normally each entire record would be on one line - if it's on multiple lines, you need some other mechanism to know where one record ends and the next begins, hence the blank line.

In either case, fgetcsv() is your man.

If you cant figure out how to use it after reading the manual page (HERE), then post back and I'm sure you'll get the assistance you need. But yeah, don't just ignore good advice because you don't get it.

Cheers
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

Hi
I wasnt ignoring it , i was experimenting with what i do know and found that was grabbing records.

if you believe the other is better i'd like to check it out .

I read about it on PHP.net I understand the logic of it being able to read lines , even give back a count on the number of lines in
CSV files . It sounds great. I looked at the examples and they seemed foriegn to what im trying to do. So I'm not sure how to
apply it to what Im trying to do yet.

If it can read and stop at the end of every line it would be perfect because that should be able to scan through address blocks
and read them all to variables of $name, $address, $city, $state $zip_code.

So technically I dont know how to make it read lines of data and convert them to variables like $name, $address etc

Thank You
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Well, the exact code is going to depend on how the data is stored in the file you are trying to import.

I mean, are we talking a file which when viewed in notepade looks like ...
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
Tom Smith,Somethin Sea Dr,Someplace City,CA, 91226,(222) 555-55555
So ... 5000+ lines and of course with all different details.

If so, then the example on the page will be right up your alley.

Code: Select all

<?php
$row = 1;
$handle = fopen("test.csv", "r");  // test csv would be the file you are trying to import
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {  // this reads one line of the file, and breaks the line at every , into an array of variables
    // This part just counts how many pieces that line was made into
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;  // this is just a simple line counter to show what line is being read
    for ($c=0; $c < $num; $c++) {  // so - for each piece of the line, show the following line.
        echo $data[$c] . "<br />\n";
    }
} // When we get here, go back and read the next line, right up to the end of the file
fclose($handle);
?> 
The internal looping and output makes it all seem a little more complex than it is. The following might be clearer.

Code: Select all

<?php
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	echo '## NEW LINE READ ##<br />\n';
	foreach($data as $id=>$value) echo '$data[' . $id . "] = $value<br />\n";
}
fclose($handle);
?> 
So long as I haven't mis-typed that, you should see a list of data down the screen. The output shows what variable name you need to use inside the for or foreach loop to access each piece of information.

If your data is in a different format, it complicates things a little, but the approach is still pretty much the same.

Hope this helps
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

thanks!

but i will need to make them broken into variables like $name, $address, $city, $state, $zip_code.

because i will be reading a file like the one you posted , and then inserting it into a database and the database will be set up similar with

record_id | name | address | city | state | zip_code

So to be able to break the variables down , so that they will be correctly inserted to the correct database fields


Thank You
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Well .. alot will depend on the state of your data. If they all have the same number of values (and they all should) then there should be no worries. If they vary, well, thats a different thing.

But, I can't think of a reason why you would need to change the names, you can just access them with the names provided. I mean, for a database write, the database doesn't care what the variable the data was stored in was.

But .. if it's that important to you, and assuming you can say for certain that the import file will always have x number of values on each line, with blanks padding any missing values, then you should be able to do something like ...

Code: Select all

list($name, $address, $city, $state, $zip_code, $phone) = $data;
... in place of the foreach line.

But yeah ... I'd really have a think about the need to do this. Why complicate issues when the data is already available, and just as reliable as the above.
Post Reply