Failing_Solutions Posted May 25, 2012 Share Posted May 25, 2012 I have a flat file that which is in this format like this 1038140, Hail of Arrows, 1, Long, Wenchratt, 25 1038144, Razorfoot Griffin, 1, Very Long, Pezfreak, 5 1038145, Razorfoot Griffin, 1, Very Long, Pezfreak, 5 1038150, Samite Healer, 1, Very Long, Pezfreak, 8 1038152, White Knight, 1, Very Long, Pezfreak, 7 1038153, White Knight, 1, Very Long, Pezfreak, 7 1038154, Boggart Harbinger, 1, Long, Pezfreak, 14 1038159, Rancor, 2, Long, Pezfreak, 12 1038163, Scattershot Archer, 1, Very Long, Pezfreak, 6 1038166, Angelic Avatar, 1, Long, Grunfell, 180 And I'm trying to parse this data so it can be inserted into my database which is basically the same format except I have 2 extra columns Here are the columns ID---Card_Name--Amount--Duration--Seller--Cost--"Price_Per"--"Date_Found" The column Price_Per is the Cost/Amount and the date is pretty simple the date. I have for the last 6 months just been taking this flat file putting it Excel and then text to columns it and add the other 2 columns of data. It only takes a few minutes but I'd like to have a way to do on my site with a form upload, parse and insert the data. I have done a lot with php but by no means am I anywhere near an expert and never used the file functions to any great degree. I'm thinking I need to put this in an array somehow.. explode it on the " , " then somehow add in the Price_Per function and just add the CURRENT_TIMESTAMP into the sql insert which I must loop through each line? Little baffled here so any help on dealing with inserting data, tips examples anything would be helpful. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/263143-looking-for-some-help-parsing-a-flat-file/ Share on other sites More sharing options...
Barand Posted May 25, 2012 Share Posted May 25, 2012 check out fgetcsv() function Quote Link to comment https://forums.phpfreaks.com/topic/263143-looking-for-some-help-parsing-a-flat-file/#findComment-1348670 Share on other sites More sharing options...
Failing_Solutions Posted May 25, 2012 Author Share Posted May 25, 2012 Ahh now that looks like a what I'm after. Going to do some test. Thank you Barand Quote Link to comment https://forums.phpfreaks.com/topic/263143-looking-for-some-help-parsing-a-flat-file/#findComment-1348672 Share on other sites More sharing options...
Failing_Solutions Posted May 25, 2012 Author Share Posted May 25, 2012 Humm, So in this I ran into another issue.. One of my cards has a comma in the name so I needed to strip that out first. So I figured this would do it.. $replace = fopen("trades.dat", 'w+'); str_replace("Eladamri, Lord of Leaves","Eladamri Lord of Leaves",'$replace'); fclose('trades.dat'); What actually happened was the file was dumped. Any suggestions on this one? Quote Link to comment https://forums.phpfreaks.com/topic/263143-looking-for-some-help-parsing-a-flat-file/#findComment-1348675 Share on other sites More sharing options...
kicken Posted May 25, 2012 Share Posted May 25, 2012 Can you re-generate the csv file so it's in a proper format? If you have fields that have a comma in them they should be surrounded by quotes so that when the file is parsed the comma is treated as part of the string rather than as a field separator. Otherwise you can fix it by using file_get_contents, str_replace then file_put_contents. Quote Link to comment https://forums.phpfreaks.com/topic/263143-looking-for-some-help-parsing-a-flat-file/#findComment-1348682 Share on other sites More sharing options...
Failing_Solutions Posted May 25, 2012 Author Share Posted May 25, 2012 Unfortunately I have no control over the output of the flat file its from a 3rd party source. I've been working on using this function to do a replace... $str=implode("\n",file('trades.dat')); $fp=fopen('trades.dat','a'); //replace something in the file string $str=str_replace('Eladamri, Lord of Leaves','Eladamri Lord of Leaves',$str); //now, rewrite the file fwrite($fp,$str); print_r($str); fclose($fp); Which works, but if I refresh the page it actually duplicates the data... still working on this. Quote Link to comment https://forums.phpfreaks.com/topic/263143-looking-for-some-help-parsing-a-flat-file/#findComment-1348685 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.