Jump to content

Reading a text file into HTML Table with PHP


jimneely

Recommended Posts

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

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
    }
}

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>

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.