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:$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]]