perl2php Posted July 8, 2007 Share Posted July 8, 2007 Hi there.. I am having a rough time trying to figure out how to open a tab-delimted .txt file into php and having it print the contents correctly. The file I have is laid out like this: Stock Item Color Price Quantity Stock Item Color Price Quantity Stock Item Color Price Quantity What I am using is: $filename = "items.txt"; $fd = fopen ($filename, "r"); $contents = fread ($fd,filesize ($filename)); fclose ($fd); $delimiter = " "; // TAB $splitcontents = explode($delimiter, $contents); ?> <font color="blue" face="arial" size="4">Split File Contents</font> <hr> <? foreach ( $splitcontents as $item ) { echo " $item<br>"; } ?> That above will cycle through and print each item.. but how do I organize it by row? So if I was going to use an HTML table to display the data, how would I tell php when to put in the new <TR> for the next row? Am I making sense? Thanks Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 8, 2007 Share Posted July 8, 2007 There is an FAQ for multi column results from a database. Although you are getting your information from a text file, the same methods still apply. Take a look at: http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Hopefully you'll be able to work it out from that. Quote Link to comment Share on other sites More sharing options...
perl2php Posted July 8, 2007 Author Share Posted July 8, 2007 That didn´t really help. I know how to sort and cycle through mysql row results.. That is not an issue. I guess a better way to describe what I am trying to is this: foreach row of tab-delimited data in items.txt separate tab-delimited data into their own scalars ($item1, $item2 etc) If I can figure out how to do that, then I accomplish more advanced things like put each row into an actual database and so on. Thanks Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 you have $contents = fread ($fd,filesize ($filename)); then what you need to do is this $items = explode("\n",$contents); //This will split at each break line foreach($items as $value){ $temp = explode("\t",$value); $item[$temp[1]] = $temp[1]; $item[$temp[1]]['stock'] = $temp[0]; $item[$temp[1]]['color'] = $temp[2]; $item[$temp[1]]['price'] = $temp[3]; $item[$temp[1]]['quanity'] = $temp[4]; } ?> This should work for you Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 8, 2007 Share Posted July 8, 2007 Was thinking along the same lines as cooldude but instead using preg_replace to replace the tabs to another symbol the explode. Quote Link to comment Share on other sites More sharing options...
perl2php Posted July 8, 2007 Author Share Posted July 8, 2007 Thanks cooldude832! I got it working. This is what I have: $file = fopen($itemfile, "r"); $contents = fread($file, filesize($itemfile)); fclose ($file); $lines = explode("\n",$contents); // Explode At Line Break foreach($lines as $line){ $items = explode("\t",$line); foreach($items as $item){ echo "$item "; } echo "<br />"; } That basically prints each line of data with each item. At the end of each line, it adds an html line break. Your help is greatly appreciated! Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 no problem yeah remember your ascii \n is new line \t is tab good only C/C++ stuff Quote Link to comment 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.