![]() |
|
[PHP] For each txt file in directory... do this... - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183) +---- Thread: [PHP] For each txt file in directory... do this... (/showthread.php?tid=5780503) |
For each txt file in directory... do this... - milknpepper - Jan 6, 2018 Been stuck on this for over 2 hours - pls assist: End goal: To turn contents of every txt file in my directory into a table. So I have .txt files with contents like: Giraffe: 1 Banana: 2 Monkey: 3 From each txt file I want it take out the "1" "2" and "3" and put it into columns (so I can later just paste it into excel and its already splitted). I was able to achieve this for ONE file at a time using the script below: PHP Code That is for alskdgj.txt, works perfect. But is it possible to do a forreach argument in the script so that it scans the directory and does this function for EVERY txt file in that directory ? So that if I have 5 files, it would eventually give me a table with the values from the 5 txt files - rather doing each one by one. Plz help. 🤝 RE: For each txt file in directory... do this... - Anxuein - Jan 6, 2018 You can use something like this to iterate: PHP Code RE: For each txt file in directory... do this... - milknpepper - Jan 6, 2018 thx will try now (Jan 6, 2018 12:25 PM)Anxuein Wrote: You can use something like this to iterate:Here is my code: PHP Code It does good for the first txt in the directory, but displays error: Quote:Notice: Undefined offset: 1 in C:\xampp\htdocs\tabled\444.php on line 17 RE: For each txt file in directory... do this... - Anxuein - Jan 6, 2018 Can you provide the contents of the first file in directory and second? RE: For each txt file in directory... do this... - milknpepper - Jan 6, 2018 (Jan 6, 2018 12:52 PM)Anxuein Wrote: Can you provide the contents of the first file in directory and second? wierd. restarted browser now it shows all 3. but still has the errors on top. Contents of files: first one: Quote:Giraffe: 1 second one: Quote:Giraffe: 12 third one Quote:Giraffe: 12 And this is what I'm seeing in the browser: Quote: RE: For each txt file in directory... do this... - Anxuein - Jan 6, 2018 Try this my dude: PHP Code RE: For each txt file in directory... do this... - milknpepper - Jan 6, 2018 (Jan 6, 2018 01:21 PM)Anxuein Wrote: Try this my dude: Almost perfect bro! It's returning me a table of 9 columns. No errors tho. Is it possible to make it 3 x 3 rather? That is; so that every txt file has its own row? RE: For each txt file in directory... do this... - Anxuein - Jan 6, 2018 PHP Code RE: For each txt file in directory... do this... - milknpepper - Jan 6, 2018 (Jan 6, 2018 01:36 PM)Anxuein Wrote: 🤗 YOU THE BEST!!! @Anxuein I'm trying to PM you but its not letting me. You really know what you're doing in php (unlike me). If you have time, would you be interested in doing a php project for btc? Its really short and simple - and this script was probably 40% of it. If ya, pls pm me. RE: For each txt file in directory... do this... - AppleJuice - Jan 6, 2018 OP, if you want to import data stored in a text file into Excel, it'd probably be easier to just parse your data and save it as CSV format (columns separated by commas) and then load it normally into Excel. |