Jump to content

[SOLVED] Tab Delimted File Reading...


perl2php

Recommended Posts

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

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.

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

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

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.