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 Link to comment https://forums.phpfreaks.com/topic/58970-solved-tab-delimted-file-reading/ 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. Link to comment https://forums.phpfreaks.com/topic/58970-solved-tab-delimted-file-reading/#findComment-292638 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 Link to comment https://forums.phpfreaks.com/topic/58970-solved-tab-delimted-file-reading/#findComment-292643 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 Link to comment https://forums.phpfreaks.com/topic/58970-solved-tab-delimted-file-reading/#findComment-292648 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. Link to comment https://forums.phpfreaks.com/topic/58970-solved-tab-delimted-file-reading/#findComment-292661 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! Link to comment https://forums.phpfreaks.com/topic/58970-solved-tab-delimted-file-reading/#findComment-292664 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 Link to comment https://forums.phpfreaks.com/topic/58970-solved-tab-delimted-file-reading/#findComment-292684 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.