Jump to content

Reading extra line in tab-delimited file


ryanbutler

Recommended Posts

Hi,

 

I'm an intemediate developer with PHP and have a question that's driving me nuts. I have a text file located here:

 

http://midwestwebdesign.net/hybrid_spring.txt

 

Basically the first line is tab-delimited, then the next is a straight line. I already read in the tab delimited array but cannot figure out a way to read in the subsequent lines. So it would read like this:

 

Tab delimited

Non-tab-delimited

Tab delimited

Non-tab-delimited

 

Here's my script so far:

 

<?php 
$theFile=fopen("web_spring.txt", "r");
$firstLine=fread($theFile,19);

<?php
//error check to ensure we can open the text file and get results back, if not, display a //warning. 
if(!$theFile)
{
    echo "Couldn't open the data file. Try again later.";
}
else
{
//read the contents of the text file
  while(!feof($theFile))
  {
//split the tab delimited file up
$data=explode("\t", fgets($theFile));
if (sizeof($data) == 14){
    
if($data[5] <= 0 && $data[6] != " LAB" && $data[12] !="Y"){ 
	print "<tr ";
	print "class=\"closed\"";
	print ">";
}
else if (trim($data[13])=="Y"){ 
 //-if you want the lab line to be yellow too then you could tack on a or here
//if ($data[12] == "Y\n" || $data[6] == " LAB"){
	print "<tr ";
	print "class=\"lab\"";
	print ">";
}

else{
	print "<tr>";

}
for ($i=0; $i<13; $i++)
{	
	//echo "<td>'$data[12]'</td>";
	print "<td>$data[$i]</td>\n";

}
print "</tr>\n";
}
}
  
  //done with the file, call the fclose function, passing variable
  fclose($theFile);
}
?>

 

The for-loop is where my current tab-delimited table cells are being parsed, so I would need to add the non-delimited line after this, which I'm presuming would be after the for-loop?

 

Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/72011-reading-extra-line-in-tab-delimited-file/
Share on other sites

Don't see that the code is that unreadable, but:

 

<?php 
$theFile=fopen("web_spring.txt", "r");
$firstLine=fread($theFile,19);
<?php
//error check to ensure we can open the text file and get results back, if not, display a //warning. 
if(!$theFile)
{
    echo "Couldn't open the data file. Try again later.";
}
else
{
//read the contents of the text file
  while(!feof($theFile))
  {
//split the tab delimited file up

$data=explode("\t", fgets($theFile));
if (sizeof($data) == 14){
if($data[5] <= 0 && $data[6] != " LAB" && $data[12] !="Y"){ 
print "<tr ";
print "class=\"closed\"";
print ">";
}
else if (trim($data[13])=="Y"){ 
//if ($data[12] == "Y\n" || $data[6] == " LAB"){
print "<tr ";
print "class=\"lab\"";
print ">";
}
else{
print "<tr>";
}
for ($i=0; $i<13; $i++)
{
//echo "<td>'$data[12]'</td>";
print "<td>$data[$i]</td>\n";
}
print "</tr>\n";
}
}  
  //done with the file, call the fclose function, passing variable
  fclose($theFile);
}
?>

Try:

<?php
$theFile=fopen("hybrid_spring.txt", "r");
$firstLine=fread($theFile,19);

//error check to ensure we can open the text file and get results back, if not, display a //warning.
if(!$theFile)
{
    echo "Couldn't open the data file. Try again later.";
}
else
{
    echo "<table cellspacing=\"2\" cellpadding=\"5\" border=\"1\">\n";

    //read the contents of the text file
    while(!feof($theFile))
    {
        //split the tab delimited file up
        $data = array_map("trim", explode("\t", fgets($theFile)));

        if (count($data) == 14)
        {
            if($data[5] <= 0 && $data[6] != "LAB" && $data[12] != "Y")
            {
                print "  <tr class=\"closed\">\n";
            }
            elseif ($data[13] == "Y")
            {
                print "  <tr class=\"lab\">\n";
            }
            else
            {
                print "  <tr>\n";
            }

            for ($i=0; $i<13; $i++)
            {
                print "    <td>$data[$i]</td>\n";
            }

            print "  </tr>\n";
        }
        elseif(!empty($data[0]))
        {
                echo "  <tr>\n    <td colspan=\"14\">" . trim($data[0]) . "</td>\n  </tr>\n";
        }
    }

    //done with the file, call the fclose function, passing variable
    fclose($theFile);

    echo '</table>';
}
?>

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.