obydesign Posted May 8, 2012 Share Posted May 8, 2012 HJi everyone. I have a script that takes base data from a csv file , parses it, and generates all the combinations that i need for my ecommerce site. It works fine as it is, but I'm trying to save myself a little time by having it generate a csv file for me when finished, instead of me copying and pasting and then exporting. Here's my code so far. Any suggestions on how to make it cleaner or more efficient would be appreciated as well. Thanks in advance. [ see code below in post 3 -- ManiacDan ] Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/ Share on other sites More sharing options...
batwimp Posted May 8, 2012 Share Posted May 8, 2012 You should put your code inside code tags to make it more readable. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344053 Share on other sites More sharing options...
obydesign Posted May 8, 2012 Author Share Posted May 8, 2012 i know its ugly. I thought i did, i apologize as i am new to posting to forums. I clicked the PHP Manual button and inserted it inbetween the tags, what did i do wrong? Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344054 Share on other sites More sharing options...
obydesign Posted May 8, 2012 Author Share Posted May 8, 2012 Hi everyone. I have a script that takes base data from a csv file , parses it, and generates all the combinations that i need for my ecommerce site. It works fine as it is, but I'm trying to save myself a little time by having it generate a csv file for me when finished, instead of me copying and pasting and then exporting. Here's my code so far. Any suggestions on how to make it cleaner or more efficient would be appreciated as well. Thanks in advance. <?php $row = 1; $first = true; if (($handle = fopen("test.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 500000, ",")) !== FALSE) { $num = 1; $row++; for ($c=0; $c < 1; $c++) { echo $data[0] . "," . $data[1] . "," . $data[2] . "," . $data[3] . "," . $data[4] . "," . $data[5] . "," . $data[6] . "," . $data[7] . "," . $data[8] . "," . $data[9] . "," . $data[10] . "," . $data[11] . "," . $data[12] . "," . $data[13] . "," . $data[14] . "," . $data[15] . "," . $data[16] . "," . $data[17] . "," . $data[18] . "," . $data[19] . "," . $data[20] . "," . $data[21] . "," . $data[22] . "," . $data[23] . "," . $data[24] . "," . $data[25] . "," . $data[26] . "," . $data[27] . "," . $data[28] . "," . $data[29] . "," . $data[30] . "," . $data[31] . "," . $data[32] . "," . $data[33] . "," . $data[34] . "," . $data[35] . "," . $data[36] . "," . $data[37] . "," . $data[38] . "," . $data[39] . "," . $data[40] . "," . $data[41] . "," . $data[42] . "," . $data[43] . "," . $data[44] . "," . $data[45] . "," . $data[46] . "," . $data[47] . "," . $data[48] . "," . $data[49] . "," . $data[50] . "," . $data[51] . "," . $data[52] . "," . $data[53] . "," . $data[54] . "," . $data[55] . "," . $data[56] . "," . $data[57] . "," . $data[58] . "," . $data[59] . "," . $data[60] . "," . $data[61] . "," . $data[62] . "," . $data[63] . "," . $data[64] . "," . $data[65] . "," . $data[66] . "," . $data[67] . "," . $data[68] . "," . $data[69] . "," . $data[70] . "," . $data[71] . "," . $data[72] ."<br />\n"; if (($handle = fopen("test.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 500000, ",")) !== FALSE) { if ($first) { $first = false; continue; } $num = 1; $row++; for ($c=0; $c < $num; $c++) { echo "admin" . ",". "base" . "," .$data[2] . "-Small-Satin-No Frame" . ",". "25.00" . ",". "Image Options" . "," . "simple" . ",".$data[6] . "," . "0" . "," .$data[8] . "-Small-Satin-No Frame" ."," . $data[9] . "," .$data[10]. "," .$data[11]. "," . $data[12] . "," .$data[13]. "," .$data[14]. "," .$data[15]. "," .$data[16] . "," . "No Layout Updates" . "," . "Product Info Column" . "," . "No". "," .$data[20]. "," .$data[21]. "," . ".9" . "," . "Enabled". "," . "None". "," . "No". "," . "1". "," . "Yes". "," . "No". "," . "Small" . "," . "Satin" . "," . "No Frame" . "," .$data[32]. "," .$data[33]. "," .$data[34]. "," .$data[35]. "," .$data[36]. "," .$data[37]. "," .$data[38]. "," .$data[39]. "," .$data[40]. "," .$data[41]. "," .$data[42]. "," . "9999". "," . "0". "," . "1". "," . "0". "," . "0". "," . "1". "," . "1". "," . "1". "," . "0". "," . "1". "," . "1". "," .$data[54]. "," .$data[55]. "," . "1". "," . "0". "," . "1". "," . "0". "," . "1". "," . "0". "," . "1". "," . "0". "," .$data[64]. "-Small-Satin-No Frame" . "," . "0". "," . "simple" . "," .$data[67] . "," .$data[68]. "," .$data[69]. "," .$data[70]. "," .$data[71]. "," .$data[72]. "<br />\n"; } } fclose($handle); } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344056 Share on other sites More sharing options...
obydesign Posted May 8, 2012 Author Share Posted May 8, 2012 ok thats better, thanks as I'm sure someone will be more likely to help if they can read the code better! Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344057 Share on other sites More sharing options...
Jessica Posted May 8, 2012 Share Posted May 8, 2012 implode for ($c=0; $c < 1; $c++) { What is the purpose of this? A loop that always executes one time....? Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344060 Share on other sites More sharing options...
obydesign Posted May 8, 2012 Author Share Posted May 8, 2012 thanks jesirose, you suggestion looks promising, but the only problem is that i am NOT a seasoned programmer and am a little unsure of how to implement "implode()". It seems that i need to have all my iterations be loaded as an array but don't know the right way to do that and then perform the "implode()" function to it. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344067 Share on other sites More sharing options...
ManiacDan Posted May 8, 2012 Share Posted May 8, 2012 Look into file_put_contents for spitting data to a file. If you use the file_append flag, you can put a f_p_c call after every echo and also spit this data out to a file. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344070 Share on other sites More sharing options...
.josh Posted May 8, 2012 Share Posted May 8, 2012 @ManiacDan: Yes, you can use file_put_contents to write to a file but that's only really useful for writing literal data. In order for him to create a csv file he's going to have to first format all the data to be csv format, before he uses file_put_contents .. which is not necessary, because that's the point of using fputcsv - this function formats the data for you. @obydesign: Did you look at the example in the manual entry for fputcsv? Was there any particular bit that you need clarification on? Basically you need to create the resource handle for the file you want to output to (same way as you created a file resource for the file you are reading in your script, only for a file to write to). Then in your for loop, instead of (or in addition to) the echo, you need to make an array out of that line of data. You will then call fputcsv with the output file resource handle as first argument, and the array of data for the row as the 2nd argument (the row data (2nd argument) is expected to be an array (so do not use implode)). Each value in the array should be the actual data you want for each column (do not wrap it in quotes or anything, unless quotes are literally part of the actual column value). // somewhere at the beginning of your file, before the loops... $outputFile = fopen('outputFileNameHere.csv', 'w'); // I assume this is the loop you are wanting to write data.... this is what your loop should look like... for ($c=0; $c < $num; $c++) { // turn that giant echo into an array... $row = array("admin","base",$data[2] ."-Small-Satin-No Frame", etc...); // write the row to the file... fputcsv($outputFile, $row); } Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344107 Share on other sites More sharing options...
xyph Posted May 9, 2012 Share Posted May 9, 2012 Look into file_put_contents for spitting data to a file. If you use the file_append flag, you can put a f_p_c call after every echo and also spit this data out to a file. On another note, you're opening a NEW resource for every write when you're using file_put_contents. It's better used for single writes, rather than writing in a loop. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344109 Share on other sites More sharing options...
ManiacDan Posted May 9, 2012 Share Posted May 9, 2012 @both OP is new enough that efficiency and overall script access isn't an issue for them. copy/pasting their echo line and changing echo to file_put_contents is the absolute fastest way to their solution. Though, OP, if you're still here: They're right. it's the least efficient method, and I should have mentioned that in my original post. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344117 Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 In the OP the stated problem is to specifically create a csv formatted file, not *a* file with data dumped into it. Sure, he *might* for the most part have little trouble taking the data format as-is and making it work elsewhere...maybe...for example, if he were to open it up in excel he could do text-to-column operation and split at comma and that will work, assuming his actual column values will never have commas in them...but that's assuming that the file is going to be used for that purpose. He may very well need it for some other script that expects a csv formatted file (he does mention having to export after c/ping) and then your method will not work at all. Since your suggestion does not solve the OP's issue, how easy/simple it is, is moot. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344120 Share on other sites More sharing options...
ManiacDan Posted May 9, 2012 Share Posted May 9, 2012 In the OP the stated problem is to specifically create a csv formatted file, not *a* file with data dumped into it. Sure, he *might* for the most part have little trouble taking the data format as-is and making it work elsewhere...maybe...for example, if he were to open it up in excel he could do text-to-column operation and split at comma and that will work, assuming his actual column values will never have commas in them...but that's assuming that the file is going to be used for that purpose. He may very well need it for some other script that expects a csv formatted file (he does mention having to export after c/ping) and then your method will not work at all. Since your suggestion does not solve the OP's issue, how easy/simple it is, is moot. OP has CSV-formatted output: echo $data[0] . "," . $data[1] . "," . $data[2] . "," . $data[3] . "," . $data[4] . "," . $data[5] . "," . $data[6] . "," . $data[7] . "," . $data[8] . "," . $data[9] . "," . $data[10] . "," . $data[11] . "," . $data[12] . "," . $data[13] . "," . $data[14] . "," . $data[15] . "," . $data[16] . "," . $data[17] . "," . $data[18] . "," . $data[19] . "," . $data[20] . "," . $data[21] . "," . $data[22] . "," . $data[23] . "," . $data[24] . "," . $data[25] . "," . $data[26] . "," . $data[27] . "," . $data[28] . "," . $data[29] . "," . $data[30] . "," . $data[31] . "," . $data[32] . "," . $data[33] . "," . $data[34] . "," . $data[35] . "," . $data[36] . "," . $data[37] . "," . $data[38] . "," . $data[39] . "," . $data[40] . "," . $data[41] . "," . $data[42] . "," . $data[43] . "," . $data[44] . "," . $data[45] . "," . $data[46] . "," . $data[47] . "," . $data[48] . "," . $data[49] . "," . $data[50] . "," . $data[51] . "," . $data[52] . "," . $data[53] . "," . $data[54] . "," . $data[55] . "," . $data[56] . "," . $data[57] . "," . $data[58] . "," . $data[59] . "," . $data[60] . "," . $data[61] . "," . $data[62] . "," . $data[63] . "," . $data[64] . "," . $data[65] . "," . $data[66] . "," . $data[67] . "," . $data[68] . "," . $data[69] . "," . $data[70] . "," . $data[71] . "," . $data[72] ."<br />\n"; He says he wants it in a file rather than having to copy/paste. The absolute quickest solution is to replace "echo" with "file_put_contents" and add the flag. Are we working off a different definition of "CSV"? Assuming there's no commas in any element of $data, it still would made made him a valid CSV file. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344209 Share on other sites More sharing options...
xyph Posted May 9, 2012 Share Posted May 9, 2012 It's the easiest until $data has values with quotes or commas Not meaning to pick a fight There's nothing 'wrong' with your solution, it's just not very robust or academic. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344229 Share on other sites More sharing options...
ManiacDan Posted May 9, 2012 Share Posted May 9, 2012 It's the easiest until $data has values with quotes or commas Not meaning to pick a fight There's nothing 'wrong' with your solution, it's just not very robust or academic. I wasn't intending it to be, and I should have clarified in my first post that it was the dirtiest possible solution. Since the OP said he was making CSVs by copying and pasting the existing output, I assumed $data had no quotes or commas. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344237 Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 OP has CSV-formatted output: ... Are we working off a different definition of "CSV"? Assuming there's no commas in any element of $data, it still would made made him a valid CSV file. That is not a valid CSV format. Column values need to be wrapped in quotes, and commas and quotes that are part of the column values (if there are any) need to be escaped. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344271 Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 Since the OP said he was making CSVs by copying and pasting the existing output, I assumed $data had no quotes or commas. My assumption is that in order for him to do that, he's having to for instance in excel perform a text-to-column operation with comma as delimiter, and just hope that the values themselves do not have commas (though he knows his data better than us, it is possible that he knows his data will NOT have commas in it). But the overall point is he asked to make a CSV file. Just dumping the existing format in a file is quick and dirty, yes. But it doesn't make for a valid CSV file. If he were to open it in excel, he would still have to perform a text-to-column operation, and if it were some other script expecting a CSV format, it would not work; the column values are not wrapped in quotes, so the script (just like excel, until you perform the text-to-column operation) would treat the entire row as one column. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344274 Share on other sites More sharing options...
ManiacDan Posted May 9, 2012 Share Posted May 9, 2012 But the overall point is he asked to make a CSV file. Just dumping the existing format in a file is quick and dirty, yes. But it doesn't make for a valid CSV file. If he were to open it in excel, he would still have to perform a text-to-column operation, and if it were some other script expecting a CSV format, it would not work; the column values are not wrapped in quotes, so the script (just like excel, until you perform the text-to-column operation) would treat the entire row as one column. I haven't used "Excel proper" in many years, but I don't recall it having problems with unquoted values. In fact, "excel style CSVs" used to be randomly quoted, only receiving quotes when necessary, and being unquoted otherwise. OpenOffice Calc opened a quick 1,2,3,4,5 test right away, it just said "this looks like a CSV, is it?" I don't have Excel, since I don't run windows. Did it stop operating like that with the recent versions? Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344288 Share on other sites More sharing options...
Jessica Posted May 9, 2012 Share Posted May 9, 2012 I'm not sure what text-to-column means, but I deal with .csv a lot and excel has never failed to open one, and automatically convert it to a .xls format with each "column" correct. You can immediately use excel features on the data. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344331 Share on other sites More sharing options...
.josh Posted May 9, 2012 Share Posted May 9, 2012 text-to-column is a data operation in excel. It's basically a wizard for letting you explode() at a specified delimiter, and put the exploded values into their own columns. Unless you've customized your menus, if you go to the "data" tab, in the 4th group you can see "Text to Columns" "Remove Duplicates" etc.. Example: in A1 if you have "a,b,c" and highlight column A and press the Text to Columns icon, it will bring up a popup wizard. You specify your delimiter, and it separates "a,b,c" into 3 separate columns so that "a" is now in A1, "b" is now in "B1", and "c" is now in "C1". As far as excel automatically converting an opened file... I can open a .csv file just fine in excel and if it is properly formatted, yes it puts things in columns, but if I do not have it formatted with quotes around columns, it puts everything into a single column... perhaps there is some kind of setting I do not have checked, that automatically converts or something..but that doesn't work for me...will check that out.. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344342 Share on other sites More sharing options...
ManiacDan Posted May 10, 2012 Share Posted May 10, 2012 I can open a .csv file just fine in excel and if it is properly formatted, yes it puts things in columns, but if I do not have it formatted with quotes around columns, it puts everything into a single column.I think this was the source of the whole disagreement. Excel never did that for me with a 1,2,3,4,5 file, no quotes. OOo pulls up a little "this looks like a plaintext spreadsheet, what's the delimiter, it looks like a comma" dialog. Quote Link to comment https://forums.phpfreaks.com/topic/262266-fputcsv-help/#findComment-1344448 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.