benjudy Posted October 31, 2007 Share Posted October 31, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/75566-build-html-table-from-text-file/ Share on other sites More sharing options...
stuffradio Posted October 31, 2007 Share Posted October 31, 2007 <? $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 Quote Link to comment https://forums.phpfreaks.com/topic/75566-build-html-table-from-text-file/#findComment-382321 Share on other sites More sharing options...
Psycho Posted October 31, 2007 Share Posted October 31, 2007 <?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++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75566-build-html-table-from-text-file/#findComment-382326 Share on other sites More sharing options...
kratsg Posted October 31, 2007 Share Posted October 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/75566-build-html-table-from-text-file/#findComment-382329 Share on other sites More sharing options...
benjudy Posted November 1, 2007 Author Share Posted November 1, 2007 stuffradio, mjdamato, kratsg -- thank you so much! You're all awesome. With your help I got it working. Quote Link to comment https://forums.phpfreaks.com/topic/75566-build-html-table-from-text-file/#findComment-382717 Share on other sites More sharing options...
benjudy Posted November 1, 2007 Author Share Posted November 1, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/75566-build-html-table-from-text-file/#findComment-382828 Share on other sites More sharing options...
kratsg Posted November 1, 2007 Share Posted November 1, 2007 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'> Quote Link to comment https://forums.phpfreaks.com/topic/75566-build-html-table-from-text-file/#findComment-382980 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.