csv to sql convert:how to evaluate CSV lines

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
esandra
Forum Newbie
Posts: 24
Joined: Sun Aug 30, 2009 11:11 pm

csv to sql convert:how to evaluate CSV lines

Post by esandra »

HI
I am very confused right now guys. I have no idea how to evaluate my upload script. This adds CSV lines into my phpmyadmin table and it works fine, but i want to make it more user friendly so users could simply `save as` an XLS file into a CSV file without making any adjustments. And so the program would do most of the work from there. So if my CSv file looks like this:

, ,, ,,,,,
some word, random, ,,,,
#Batch 312-313
312,John Smith, New Jersey,, //line 1
313, Ana White, California,, //line 2
#Batch 314-315
314, Greg Long, Mexico //line 3
315, Jane Snow, Texas,, //line 4
Prepared by:, Jack Brown, CPA


I need help on how to add only the four lines into the fields: `number`, `name`, `address`
How do i disregard any other things in the CSV except for words that should be on my database?
Thank you for your time, and have a nice day.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: csv to sql convert:how to evaluate CSV lines

Post by requinix »

It looks like you want only the lines that
a) Begin after the first "#Batch" line
b) Have a number in the first field

So

Code: Select all

open the file
while not at the end of the file {
    read a line with fgets
    if the line starts with "#Batch" then exit this loop
}
while not at the end of the file {
    read a line using fgetcsv
    if the size of the returned array is at least 3 and the first item is a number then {
        do whatever you want with the data
    } else if the first item is not a number and does not start with "#Batch" then {
        exit this loop
    }
}
close the file
esandra
Forum Newbie
Posts: 24
Joined: Sun Aug 30, 2009 11:11 pm

Re: csv to sql convert:how to evaluate CSV lines

Post by esandra »

Thanks tasairis, i'll try this out
Post Reply