jimneely Posted June 6, 2014 Share Posted June 6, 2014 (edited) Okay I don't mind telling you I am very perplexed on how to read a text file delimited with TAB into a HTML Table using PHP. I don't know where to begin, I have read about 12 examples and get more confused each time. I hope someone can help me. I am looking to read this delimited (TAB) file skipping the first line and reading array's 2,5,6,7,8,9, and 10, then displaying in a HTML Table. Corresponding Headers would be nice. The php file does not work at the present because I am trying to read the array sections by row. Thank You in advance ParseText.php JimTest.txt Edited June 6, 2014 by jimneely Quote Link to comment https://forums.phpfreaks.com/topic/289008-reading-a-text-file-into-html-table-with-php/ Share on other sites More sharing options...
Solution Ch0cu3r Posted June 6, 2014 Solution Share Posted June 6, 2014 (edited) Ok you're on the right track. The problem is with line 6 list = explode(' ',$line);//you get an array of 4 per line, first item is author, etc... list should be $list you forgot the dollar sign Next problem is your data is separated by tabs, not a single space character. To explode the data using tabs you can use the \t escape character inside double quotes. Line 6 should now read as $list = explode("\t",$line); // explode the data by tabs if you only want to read the first 11 lines and ignore the first line you can create a simple counter function get_rows() { $file=fopen("jimtest.txt",'r'); $i = 0; // initiate counter while($line = fgets($file)) //while we can still read the file { $i++; // increments $i by one on each iteration of the loop // if the counter $i is equal to zero (meaning were reading the first line of the text file) if($i == 0 ) continue; // skip to the next iteration (which will read the next line in he file) // else if the counter $i is greater than 11 (meaning were now reading line 11) else if($i > 11) break; //terminate the while loop (stops reading the remain lines in the file) $list = explode("\t", trim($line)); // remove the line endings and extra white-spaces at start and end, then explode the line contents by tabs echo "<tr><td>$list[2]</td><td>$list[5]</td><td>$list[6]</td><td>$list[7]</td><td>$list[8]</td><td>$list[9]</td><td>$list[10]</td></tr>\n";//use the \n to clean up the source a code a bit to make it readable } } Edited June 6, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/289008-reading-a-text-file-into-html-table-with-php/#findComment-1482021 Share on other sites More sharing options...
jimneely Posted June 6, 2014 Author Share Posted June 6, 2014 Thanks for the help. I still can not get the HTML Table to echo out to the browser. It will display the Table Header stuff and that's all. Any suggestions??? Quote Link to comment https://forums.phpfreaks.com/topic/289008-reading-a-text-file-into-html-table-with-php/#findComment-1482032 Share on other sites More sharing options...
Barand Posted June 6, 2014 Share Posted June 6, 2014 Alternative method giving table headings from first row in csv file <table border=1 cellpadding="4" style="border-collapse: collapse;"> <?php $fdata = file('test.csv', FILE_IGNORE_NEW_LINES); echo '<tr><th>' . join('</th><th>', str_getcsv($fdata[0],"\t")) . '</th></tr>'; // headings $required = array(2,5,6,7,8,9,10); foreach ($required as $n) { echo '<tr><td>' . join('</td><td>', str_getcsv($fdata[$n], "\t")) . '</td></tr>'; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/289008-reading-a-text-file-into-html-table-with-php/#findComment-1482038 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.