Droplet -> TabTextToHTMLTable

Argos

#9
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?
Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
----------------------------------------------------------------
Please don't request personal support, use the forums!

Argos

Potentially very useful droplet, thanks for sharing!!
Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
----------------------------------------------------------------
Please don't request personal support, use the forums!

crnogorac081

wow, cool I like this.. it can be used for everything :)
Web developer

Stefek

Hello,

I see what you mean.

So thanks again for explanation and sharing.

It's indeed a useful dropleto.

Regards,
Stefek
[i]"Gemeinsam schafft man mehr."[/i]

[b][url=http://duden.de/rechtschreibung/gemeinsam#Bedeutung1]gemeinsam[/url][/b]
1. mehreren Personen oder Dingen in gleicher Weise gehörend, eigen
2. in Gemeinschaft [unternommen, zu bewältigen]; zusammen, miteinander
#Duden

marathoner

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.

Stefek

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
[i]"Gemeinsam schafft man mehr."[/i]

[b][url=http://duden.de/rechtschreibung/gemeinsam#Bedeutung1]gemeinsam[/url][/b]
1. mehreren Personen oder Dingen in gleicher Weise gehörend, eigen
2. in Gemeinschaft [unternommen, zu bewältigen]; zusammen, miteinander
#Duden

marathoner

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.
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:
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:

Stefek

Hello Marathoner,
thanks for sharing.

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

Regards,
Stefek
[i]"Gemeinsam schafft man mehr."[/i]

[b][url=http://duden.de/rechtschreibung/gemeinsam#Bedeutung1]gemeinsam[/url][/b]
1. mehreren Personen oder Dingen in gleicher Weise gehörend, eigen
2. in Gemeinschaft [unternommen, zu bewältigen]; zusammen, miteinander
#Duden

marathoner

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