WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Droplets & Snippets => Topic started by: marathoner on March 14, 2010, 08:40:29 PM

Title: Droplet -> TabTextToHTMLTable
Post by: marathoner on March 14, 2010, 08:40:29 PM
I occasionally need to post race results on my site in a HTML table. In order to make like easier I created a droplet that will read a tab-delimited text file (uploaded to the MEDIA directory) and create a HTML table. This droplet takes the 4 following parameters:

Required:
file - the filename in the MEDIA directory
Optional:
class - the CSS class used for the table
id - the CSS id used for the table
headerrow - set to TRUE if you want the cells in the first row of the HTML table to use <th> rather than <td>

Droplet name: TabTextToHTMLTable
Description: Reads tab delimited text file and converts to HTML table
Code:
Code: [Select]
$returnvalue = "";
$filepath = WB_PATH.MEDIA_DIRECTORY.'/'.$file;
$fileurl = WB_URL.MEDIA_DIRECTORY.'/'.$file;
if(file_exists($filepath))
  {
  $lines = file($fileurl);
  if (!empty($class)) $tableclass = " class=\"$class\"";
  if (!empty($id)) $tableid = " id=\"$id\"";
  $returnvalue .= '<table'.$tableclass.$tableid.">\n";
  foreach ($lines as $line_num => $line) {
    $returnvalue .= "<tr>\n";
    $data = explode("\t", $line);
    foreach ($data as $cell) {
      if (($line_num===0) && ($headerrow==="TRUE")) {$returnvalue .= '<th>'.$cell."</th>\n";}
        else {$returnvalue .= '<td>'.$cell."</td>\n";}
      }
    $returnvalue .= "</tr>\n";
    }
  $returnvalue .= "</table>\n";
  }
else {$returnvalue = "File not found - ". $filepath;}
return $returnvalue;
Comments: Commandline to use:
[[TabTextToHTMLTable?file=filename_in_mediafolder&class=tableclass&id=tableid&headerrow=TRUE]]
Title: Re: Droplet -> TabTextToHTMLTable
Post by: Stefek on March 14, 2010, 09:21:00 PM
Hello Marathoner,
thanks for sharing.

Maybe it was helpful, if you upload a example file too.

Regards,
Stefek
Title: Re: Droplet -> TabTextToHTMLTable
Post by: marathoner on March 15, 2010, 12:09:27 AM
Example tab delimited file named "Results.txt" uploaded to MEDIA directory. Note that there are four fields separated by tab. I have used the word {TAB} in this example to represent the tab character and does not actually exist in Results.txt. The original file created by saving an Excel spreadsheet but could have been created using any tool that can save a tab delimited text file.
Code: [Select]
First Name{TAB}Last Name{TAB}Age{TAB}Gender{TAB}Time
Bob{TAB}White{TAB}31{TAB}M{TAB}18:37
Betty{TAB}Wont{TAB}32{TAB}F{TAB}19:50
Justin{TAB}Case{TAB}50{TAB}M{TAB}22:12

Here is the WYSIWYG page:
Code: [Select]
Results from the Test My Patience 5K run:

[[TabTextToHTMLTable?file=Results.txt&class=stripe&headerrow=TRUE]]

Combined with some CSS and jquery the resulting table is striped (alternating colors on odd/even rows) and looks like this:
(http://www.uberrunner.com/media/TabTextToHTMLTable.png)
Title: Re: Droplet -> TabTextToHTMLTable
Post by: Stefek on March 15, 2010, 12:24:33 AM
Hello Marathoner,

thanks a lot for your explanations and examples.

Nowit makes sense to me.

Would the droplet work also with a Comma Separated Value (CSV) if I replace /t by ',' in the explode function (line 12)?

Never worked with CSV or tab delimited text files in this way..

Regards,
Stefek
Title: Re: Droplet -> TabTextToHTMLTable
Post by: marathoner on March 15, 2010, 01:22:31 AM
Sure, you could use any delimiter. Be aware that some CSV formats wrap double-quotes around fields that contain a comma or other special characters so additional logic may be needed.
Title: Re: Droplet -> TabTextToHTMLTable
Post by: Stefek on March 15, 2010, 01:52:42 AM
Hello,

I see what you mean.

So thanks again for explanation and sharing.

It's indeed a useful dropleto.

Regards,
Stefek
Title: Re: Droplet -> TabTextToHTMLTable
Post by: crnogorac081 on March 15, 2010, 01:58:26 PM
wow, cool I like this.. it can be used for everything :)
Title: Re: Droplet -> TabTextToHTMLTable
Post by: Argos on March 19, 2010, 01:34:36 AM
Potentially very useful droplet, thanks for sharing!!
Title: Re: Droplet -> TabTextToHTMLTable
Post by: Argos on July 10, 2010, 12:12:58 PM
I just tried this droplet, but the droplet admin says that it contains invalid PHP code, and it shows the red icon. I just copied/pasted it without any modifications. I checked and double checked. Other droplets work fine. I use WB2.7 on this site.

On my WB2.8 testsite it works fine, so maybe it's not WB2.7 compatible? If not, can it made so that it supports WB2.7 too?