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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.