ryanbutler Posted October 5, 2007 Share Posted October 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72011-reading-extra-line-in-tab-delimited-file/ Share on other sites More sharing options...
Yesideez Posted October 5, 2007 Share Posted October 5, 2007 If you enclose your code using CODE tags it will be MUCH easier to read. Quote Link to comment https://forums.phpfreaks.com/topic/72011-reading-extra-line-in-tab-delimited-file/#findComment-362785 Share on other sites More sharing options...
ryanbutler Posted October 5, 2007 Author Share Posted October 5, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72011-reading-extra-line-in-tab-delimited-file/#findComment-362891 Share on other sites More sharing options...
wildteen88 Posted October 5, 2007 Share Posted October 5, 2007 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72011-reading-extra-line-in-tab-delimited-file/#findComment-362893 Share on other sites More sharing options...
ryanbutler Posted October 8, 2007 Author Share Posted October 8, 2007 Perfect...that worked. Thanks a lot for the help! Quote Link to comment https://forums.phpfreaks.com/topic/72011-reading-extra-line-in-tab-delimited-file/#findComment-364571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.