Jump to content

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>';
}
?>

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.