How to parse CSV file in PHP and store fields in a database? -
i need parsing following csv file in php, can insert contents database.
i know use file_get_contents() after feel bit lost.
what i'd store.
- collection1 - events.text & date
- collection2 - position & name.text & total
i'm not sure how best structure data insert database table.
"**collection1**" "events.href","**events.text**","**date**","index","url" "tur.com/events/classic.html","john deere classic","thursday jul 9 - sunday jul 12, 2015","1","tur.com/r.html" "collection2" "**position**","name.href","**name.text**","**total**","index","url" "--","javascript:void(0);","scott","--","2","tur.com/r.html" "--","javascript:void(0);","billy","--","3","tur.com/r.html" "--","javascript:void(0);","jon","--","4","tur.com/r.html" "--","javascript:void(0);","bill","--","5","tur.com/r.html" "--","javascript:void(0);","tim","--","6","tur.com/r.html" "--","javascript:void(0);","carlos","--","7","tur.com/r.html" "--","javascript:void(0);","robert","--","8","tur.com/r.html" "--","javascript:void(0);","rod","--","9","tur.com/r.html"
as per your previous question, think needs broken down sections. stands rather broad answer.
- read information using
file_get_contents(). make sure works first, echoing console. (it sounded other question felt not work if url not have.csvsuffix. should work regardless of file extension - try it. if fails may dependent on cookies or javascript or other problem). - design , create table structure in mysql. seems have 2 tables. should both have primary key. related in fashion? if so, perhaps 1 has foreign key other one?
- explode text file on new line character , loop across resulting array of lines.
- if csv data has title row in first row position, delete array.
- for each line, read elements of interest using php's build-in csv parsing functions, , store them in variables.
- pass these variables custom function saves data.
- for each save, you'll need
insert. recommend using pdo here. make sure bind parameters.
where stuck on specific problem, can ask new , focussed question. @ present, task break things down discrete , researchable pieces.
one trick worth remembering shortcut php manual. if not know how fgetcsv works, example, type php.net/fgetcsv browser address bar, , php site find function you. documentation excellent.
Comments
Post a Comment