Jump to content

Build HTML table from text file


benjudy

Recommended Posts

Sorry -- this might be somewhat of a newbie question, so bear with me. (Then again, it should be easy to answer!) What can I say, I'm learning.

 

I'm trying to dynamically build an HTML table from a flat text file.

 

My text file consists of four lines:

    Tom Sayer

    Mark Twain

    Jan. 1, 2007

    Jan 20, 2007

My HTML table has a header row that looks like this:

    <tr>

        <th>Title</th>

        <th>Author(s)</th>

        <th>Started</th>

        <th>Finished</th>

    </tr>

So you can see that the data in the text file corresponds to these headers. (It's a book-reading journal).

 

I found a code snippet which helped me pull data from the text file and echo it into table cells. Pretty straightforward.

    <tr>

    <?

        $text = file('reading.txt');

        // loop through array and output contents

        foreach($text as $chunk) {

            echo "<td>" . $chunk . "</td>\n";

        }

    ?>

    </tr>

Works beautifully. Here comes the part I need help with.

 

As you might guess, if I add another row of data to my text file, I want the HTML table to close the row and start a new one, so there are four columns in each row.

 

So if my text file would look like this:

 

    Tom Sayer

    Mark Twain

    Jan. 1, 2007

    Jan 20, 2007

    Great Expectations

    Charles Dickens

    Feb. 5, 2007

    Feb. 8, 2007

 

Currently, if I do this I just end up with eight table cells in the same row. What do I need to do in the PHP to tell it to start a new row after every fourth cell? Some sort of incrementing I guess...?

 

Thanks!

Link to comment
Share on other sites

 

    <?

        $text = file('reading.txt');

        $row = 4;

        // loop through array and output contents

        foreach($text as $chunk) {

            echo "<tr><td>" . $chunk . "</td></tr>\n";

    if ($row == 0) {

      echo "<br />";

      }

        }

    ?>

   

 

That will and should do it for you :)

Link to comment
Share on other sites

<?php

$rows = 4;
$current_row = 1;
$text = file('reading.txt');

// loop through array and output contents
foreach($text as $chunk) {
   if ($current_row%$rows==1) { echo "<tr>\n"; }
   echo "<td>$chunk</td>\n";
   if ($current_row%$rows==0) { echo "</tr>\n"; }
   $current_row++;
}

?>

Link to comment
Share on other sites

Me being a bit of a perfectionist, I need to explain why both codes submitted are going to have problems later on. You are parsing the \n as well, here's what I would suggest in order to ignore the 4 row rule (as there may be a time when you have to parse a blank line, you'll get an error).

 

Assume the text file looks like this (I prefer this method):

 

books.txt

Tom Sayer|Mark Twain|Jan. 1, 2007|Jan 20, 2007
Great Expectations|Charles Dickens|Feb. 5, 2007|Feb. 8, 2007

 

table_parse.php

<?php
function get_rows() {
$file=fopen("reading.txt",'r');
while($line = fgets($file)){//while we can still read the file
	$line=trim($line);//remove the line endings and extra white-spaces at start and end
	list($title,$author,$started,$ended) = explode('|',$line);//you get an array of 4 per line, first item is author, etc...
	echo "<tr><td>$title</td><td>$author</td><td>$started</td><td>$ended</td></tr>\n";//use the \n to clean up the source a code a bit to make it readable
}
return true;
}
?>

//everything is the regular html stuff.
<table>
    <tr>
        <th>Title</th>
        <th>Author(s)</th>
        <th>Started</th>
        <th>Finished</th>
    </tr>
<?php get_rows(); ?>
</table>

 

I prefer this method because it saves on the number of rows in the text file, making the script a little faster in reading line by line (as there aren't that many lines to read). It's more compact, making it cleaner, and sleeker (especially since you trim() the outputs from the file). Any questions, comments, suggestions?

 

Edit: I commented everything out almost line by line so you can get a clear grasp of what I'm doing here.

Link to comment
Share on other sites

OK, extending this a bit... say I wanted an unique id on each table cell. So the output would look something like this:

    <tr>

        <td id="title-1">Tom Sayer</td>

        <td id="author-1">Mark Twain</td>

        <td id="started-1">Jan. 1, 2007</td>

        <td id="ended-1">Jan 20, 2007</td>

    </tr>

    <tr>

        <td id="title-2">Great Expectations</td>

        <td id="author-2">Charles Dickens</td>

        <td id="started-2">Feb. 5, 2007</td>

        <td id="ended-2">Feb. 8, 2007</td>

    </tr>

...and so on...

 

Using the list method provided by kratsg, how can I do that? Are there numeric keys associated with each item in the list? I've played around with the key() function but all I get is errors. (By the way, the reason I need unique id's is so I can add some edit-in-place AJAX.)

 

Thanks so much for your help!

Link to comment
Share on other sites

Automatic and unique IDs.

 

books.txt

Tom Sayer|Mark Twain|Jan. 1, 2007|Jan 20, 2007
Great Expectations|Charles Dickens|Feb. 5, 2007|Feb. 8, 2007

 

table_parse.php

<?php
function get_rows() {
$i = 0;
$file=fopen("reading.txt",'r');
while($line = fgets($file)){//while we can still read the file
	$i++;
	$line=trim($line);//remove the line endings and extra white-spaces at start and end
	list($title,$author,$started,$ended) = explode('|',$line);//you get an array of 4 per line, first item is author, etc...
	echo "<tr><td id='title-$i'>$title</td><td id='author-$i'>$author</td><td id='started-$i'>$started</td><td id='ended-$i'>$ended</td></tr>\n";//use the \n to clean up the source a code a bit to make it readable
}
return true;
}
?>

//everything is the regular html stuff.
<table>
    <tr>
        <th>Title</th>
        <th>Author(s)</th>
        <th>Started</th>
        <th>Finished</th>
    </tr>
<?php get_rows(); ?>
</table>

 

Added: $i=0;

$i++;

<td id='title-$i'>

<td id='author-$i'>

<td id='started-$i'>

<td id='ended-$i'>

Link to comment
Share on other sites

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.