WebsiteBaker Logo
  • *
  • Templates
  • Help
  • Add-ons
  • Download
  • Home
*
Welcome, Guest. Please login or register.

Login with username, password and session length
 

News


WebsiteBaker 2.13.6 is now available!


Will it continue with WB? It goes on! | Geht es mit WB weiter? Es geht weiter!
https://forum.websitebaker.org/index.php/topic,32340.msg226702.html#msg226702


The forum email address board@websitebaker.org is working again
https://forum.websitebaker.org/index.php/topic,32358.0.html


R.I.P Dietmar (luisehahne) and thank you for all your valuable work for WB
https://forum.websitebaker.org/index.php/topic,32355.0.html


* Support WebsiteBaker

Your donations will help to:

  • Pay for our dedicated server
  • Pay for domain registration
  • and much more!

You can donate by clicking on the button below.


  • Home
  • Help
  • Search
  • Login
  • Register

  • WebsiteBaker Community Forum »
  • WebsiteBaker Support (2.8.x) »
  • Droplets & Snippets »
  • Droplet -> TabTextToHTMLTable
  • Print
Pages: [1]   Go Down

Author Topic: Droplet -> TabTextToHTMLTable  (Read 6667 times)

Offline marathoner

  • Posts: 495
Droplet -> TabTextToHTMLTable
« 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]]
Logged

Offline Stefek

  • Posts: 6177
  • Gender: Male
  • ("ړ)
Re: Droplet -> TabTextToHTMLTable
« Reply #1 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
Logged
"Gemeinsam schafft man mehr."

gemeinsam
1. mehreren Personen oder Dingen in gleicher Weise gehörend, eigen
2. in Gemeinschaft [unternommen, zu bewältigen]; zusammen, miteinander
#Duden

Offline marathoner

  • Posts: 495
Re: Droplet -> TabTextToHTMLTable
« Reply #2 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:
Logged

Offline Stefek

  • Posts: 6177
  • Gender: Male
  • ("ړ)
Re: Droplet -> TabTextToHTMLTable
« Reply #3 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
Logged
"Gemeinsam schafft man mehr."

gemeinsam
1. mehreren Personen oder Dingen in gleicher Weise gehörend, eigen
2. in Gemeinschaft [unternommen, zu bewältigen]; zusammen, miteinander
#Duden

Offline marathoner

  • Posts: 495
Re: Droplet -> TabTextToHTMLTable
« Reply #4 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.
Logged

Offline Stefek

  • Posts: 6177
  • Gender: Male
  • ("ړ)
Re: Droplet -> TabTextToHTMLTable
« Reply #5 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
Logged
"Gemeinsam schafft man mehr."

gemeinsam
1. mehreren Personen oder Dingen in gleicher Weise gehörend, eigen
2. in Gemeinschaft [unternommen, zu bewältigen]; zusammen, miteinander
#Duden

Offline crnogorac081

  • Posts: 2161
  • Gender: Male
Re: Droplet -> TabTextToHTMLTable
« Reply #6 on: March 15, 2010, 01:58:26 PM »
wow, cool I like this.. it can be used for everything :)
Logged
Web developer

Argos

  • Guest
Re: Droplet -> TabTextToHTMLTable
« Reply #7 on: March 19, 2010, 01:34:36 AM »
Potentially very useful droplet, thanks for sharing!!
Logged

Argos

  • Guest
Re: Droplet -> TabTextToHTMLTable
« Reply #8 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?
« Last Edit: July 10, 2010, 12:16:04 PM by Argos »
Logged

  • Print
Pages: [1]   Go Up
  • WebsiteBaker Community Forum »
  • WebsiteBaker Support (2.8.x) »
  • Droplets & Snippets »
  • Droplet -> TabTextToHTMLTable
 

  • SMF 2.0.19 | SMF © 2017, Simple Machines
  • XHTML
  • RSS
  • WAP2