output HTML table from 2D array

Share your advanced PureBasic knowledge/code with the community.
jamba
Enthusiast
Enthusiast
Posts: 144
Joined: Fri Jan 15, 2010 2:03 pm
Location: Triad, NC
Contact:

output HTML table from 2D array

Post by jamba »

I use this to quickly store a table in a 2D structured array, and then output it with the function below.
It can be made more flexible, but this is how I use it for what I need to right now.

I haven't expanded on it too much, but it is pretty easy to do so and I'm sure I'll continue to add to it as I use more features.

*note: this does not handle the beginning or ending <html>, <body> or other tags or things like CSS (although it does support class within the td tags). this is for a simple table ONLY.

*note2: due to this arraysize bug with PB 4.5b2, the output will not be complete

Code: Select all



Enumeration
  #FT_Align_Right
  #FT_Align_Center
  #FT_Align_Left

EndEnumeration

Structure FormattedText
  text.s
  bgColor.s  
  fontColor.s
  fontSize.i
  font.s
  align.i  ;left/right/center
EndStructure

Procedure.s HTMLcomment(txt.s)
  ;returns text commented out the HTML way

  ProcedureReturn "<!-- " + txt + " -->"
EndProcedure

Procedure HTML_PrintTable(Array arr.FormattedText(2), nFile.i, nWidth.i = 35, class.s="", firstRowIsHdr.i = 0) 
  Protected.i nr, nc, Padding=1
  Protected line.s
  
  If Not nFile : ProcedureReturn : EndIf
  
  line = "<table"
  If class
    line = line + " class=" + #DQUOTE$ + class + #DQUOTE$
  EndIf
  line = line + " border=1 width=" + Str(nWidth) + "%>"
  
  WriteStringN(nFile,line)
  
  line = ""
  WriteStringN(nFile,HTMLcomment("Start loop for rows"))
  For nr = 0 To ArraySize(arr(),2)
    If Not firstRowIsHdr
      WriteStringN(nFile,"<tr>")
    Else
      WriteStringN(nFile,"<th>")
    EndIf
    WriteStringN(nFile,HTMLcomment("loop for column cells"))
    For nc = 0 To ArraySize(arr(),1)
      line = "<td padding=" + Str(Padding)
      If class
        line = line + " class=" + #DQUOTE$ + class + #DQUOTE$
      EndIf
      If arr(nc,nr)\bgColor
        line = line + " bgcolor=" + #DQUOTE$ + arr(nc,nr)\bgColor + #DQUOTE$
      EndIf
      Select arr(nc,nr)\align
      Case #FT_Align_Center  
        line = line + " align=" + #DQUOTE$ + "center" + #DQUOTE$
      Case #FT_Align_Left  
        line = line + " align=" + #DQUOTE$ + "left" + #DQUOTE$
      Case #FT_Align_Right  
        line = line + " align=" + #DQUOTE$ + "right" + #DQUOTE$
      Default  
      EndSelect
      line = line + ">"
      If arr(nc,nr)\font Or arr(nc,nr)\fontSize Or arr(nc,nr)\fontColor
        line = line + "<font" 
        If arr(nc,nr)\font
          line = line + " " + arr(nc,nr)\font
        EndIf
        If arr(nc,nr)\fontSize
          line = line + " size=" + Str(arr(nc,nr)\fontSize)
        EndIf
        If arr(nc,nr)\fontColor
          line = line + " color=" + arr(nc,nr)\fontColor
        EndIf
        line = line + ">"
      EndIf
      line = line + arr(nc,nr)\text
      If arr(nc,nr)\font Or arr(nc,nr)\fontSize Or arr(nc,nr)\fontColor
        line = line + "</font>"
      EndIf
      line = line + "</td>"
      WriteStringN(nFile,line)
      line = ""
    Next nc
    If Not firstRowIsHdr
      WriteStringN(nFile,"</tr>")
    Else
      WriteStringN(nFile,"</th>")
    EndIf
  Next nr
  WriteStringN(nFile,"</table>")
EndProcedure

Dim html.FormattedText(9,9)

For c.i = 0 To 9
  For r.i = 0 To 9
    With html(c,r)  
      \text = "row: " + Str(r) + ", col: " + Str(c)
      \align = #FT_Align_Center
      \fontColor = "blue"
    EndWith
  Next r
Next c

fname.s = GetTemporaryDirectory() + "html_output.htm"
fnum.i = CreateFile(#PB_Any, fname)
If fnum
  WriteStringN(fnum,"<html><title>test</title><body>")
  HTML_PrintTable(html(),fnum)
  WriteStringN(fnum,"</body></html>")
  CloseFile(fnum)
  
  RunProgram("notepad.exe",fname,GetTemporaryDirectory())
EndIf


-Jon

Fedora user
But I work with Win7
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Re: output HTML table from 2D array

Post by epidemicz »

Very cool man, I just used something like this the other day and its definitely very handy. Although, I didn't write it - it's a feature in toad you can dump a sql query to a html table.

Couldn't resist hacking it to have alternating row colors :D. I think I had to change a few other things to compile it in this version(4.30). Very cool, thanks again.

Code: Select all

Enumeration
  #FT_Align_Right
  #FT_Align_Center
  #FT_Align_Left

EndEnumeration

Structure FormattedText
  text.s
  bgColor.s  
  fontColor.s
  fontSize.i
  font.s
  align.i  ;left/right/center
EndStructure

Procedure.s HTMLcomment(txt.s)
  ;returns text commented out the HTML way

  ProcedureReturn "<!-- " + txt + " -->"
EndProcedure

Procedure HTML_PrintTable(Array arr.FormattedText(2), nFile.i, nWidth.i = 35, class.s="", firstRowIsHdr.i = 0) 
  Protected nr.i
  Protected nc.i
  Protected Padding.i=1
  Protected line.s
  Protected RowColor1.s = "FFFF99"
  Protected RowColor2.s = "FFFFCC"
  
  If Not nFile : ProcedureReturn : EndIf
  
  line = "<table"
  If class
    line = line + " class=" + #DQUOTE$ + class + #DQUOTE$
  EndIf
  line = line + " border=1 width=" + Str(nWidth) + "%>"
  
  WriteStringN(nFile,line)
  
  line = ""
  WriteStringN(nFile,HTMLcomment("Start loop for rows"))
  For nr = 0 To ArraySize(arr(),2)
    If Not firstRowIsHdr
      If nr%2 = 1
        WriteStringN(nFile,"<tr bgcolor=" + Chr(34) + RowColor1 + Chr(34) + ">")
      Else
        WriteStringN(nFile,"<tr bgcolor=" + Chr(34) + RowColor2 + Chr(34) + ">")
      EndIf  
    Else
      WriteStringN(nFile,"<th>")
    EndIf
    WriteStringN(nFile,HTMLcomment("loop for column cells"))
    For nc = 0 To ArraySize(arr(),1)
        line = "<td padding=" + Str(Padding)
      If class
        line = line + " class=" + #DQUOTE$ + class + #DQUOTE$
      EndIf
      If arr(nc,nr)\bgColor
        line = line + " bgcolor=" + #DQUOTE$ + arr(nc,nr)\bgColor + #DQUOTE$
      EndIf
      Select arr(nc,nr)\align
      Case #FT_Align_Center  
        line = line + " align=" + #DQUOTE$ + "center" + #DQUOTE$
      Case #FT_Align_Left  
        line = line + " align=" + #DQUOTE$ + "left" + #DQUOTE$
      Case #FT_Align_Right  
        line = line + " align=" + #DQUOTE$ + "right" + #DQUOTE$
      Default  
      EndSelect
      line = line + ">"
      If arr(nc,nr)\font Or arr(nc,nr)\fontSize Or arr(nc,nr)\fontColor
        line = line + "<font" 
        If arr(nc,nr)\font
          line = line + " " + arr(nc,nr)\font
        EndIf
        If arr(nc,nr)\fontSize
          line = line + " size=" + Str(arr(nc,nr)\fontSize)
        EndIf
        If arr(nc,nr)\fontColor
          line = line + " color=" + arr(nc,nr)\fontColor
        EndIf
        line = line + ">"
      EndIf
      line = line + arr(nc,nr)\text
      If arr(nc,nr)\font Or arr(nc,nr)\fontSize Or arr(nc,nr)\fontColor
        line = line + "</font>"
      EndIf
      line = line + "</td>"
      WriteStringN(nFile,line)
      line = ""
    Next nc
    If Not firstRowIsHdr
      WriteStringN(nFile,"</tr>")
    Else
      WriteStringN(nFile,"</th>")
    EndIf
  Next nr
  WriteStringN(nFile,"</table>")
EndProcedure

Dim html.FormattedText(9,9)

For c.i = 0 To 9
  For r.i = 0 To 9
    With html(c,r)  
      \text = "row: " + Str(r) + ", col: " + Str(c)
      \align = #FT_Align_Center
      \fontColor = "blue"
    EndWith
  Next r
Next c

fname.s = GetTemporaryDirectory() + "html_output.htm"
fnum.i = CreateFile(#PB_Any, fname)
If fnum
  WriteStringN(fnum,"<html><title>test</title><body>")
  HTML_PrintTable(html(),fnum)
  WriteStringN(fnum,"</body></html>")
  CloseFile(fnum)
  
  RunProgram("notepad.exe",fname,GetTemporaryDirectory())
EndIf
Image
jamba
Enthusiast
Enthusiast
Posts: 144
Joined: Fri Jan 15, 2010 2:03 pm
Location: Triad, NC
Contact:

Re: output HTML table from 2D array

Post by jamba »

Thanks :)

and cool on the alternating row colors idea, I think I will add this into my version also.

made it an option in the arguments ;)

Code: Select all

Enumeration
  #FT_Align_Right
  #FT_Align_Center
  #FT_Align_Left

EndEnumeration

Structure FormattedText
  text.s
  bgColor.s 
  fontColor.s
  fontSize.i
  font.s
  align.i  ;left/right/center
EndStructure

Procedure.s HTMLcomment(txt.s)
  ;returns text commented out the HTML way

  ProcedureReturn "<!-- " + txt + " -->"
EndProcedure

Procedure HTML_PrintTable(Array arr.FormattedText(2), nFile.i, nWidth.i = 35, class.s="", firstRowIsHdr.i = 0, AlternateRowColors.i = 0) 
  Protected.i nr, nc
  Protected.i Padding=1
  Protected line.s
  Protected RowColor1.s = "FFFF99"
  Protected RowColor2.s = "FFFFCC"
   
  If Not nFile : ProcedureReturn : EndIf
  
  line = "<table"
  If class
    line = line + " class=" + #DQUOTE$ + class + #DQUOTE$
  EndIf
  line = line + " border=1 width=" + Str(nWidth) + "%>"
  
  WriteStringN(nFile,line)
  
  line = ""
  WriteStringN(nFile,HTMLcomment("Start loop for rows"))
  For nr = 0 To ArraySize(arr(),2)
    If Not firstRowIsHdr
      If AlternateRowColors
        If nr%2 = 1
          WriteStringN(nFile,"<tr bgcolor=" + Chr(34) + RowColor1 + Chr(34) + ">")
        Else
          WriteStringN(nFile,"<tr bgcolor=" + Chr(34) + RowColor2 + Chr(34) + ">")
        EndIf 
      Else
        WriteStringN(nFile,"<tr>")
      EndIf
    Else
      If AlternateRowColors
        If nr%2 = 1
          WriteStringN(nFile,"<th bgcolor=" + Chr(34) + RowColor1 + Chr(34) + ">")
        Else
          WriteStringN(nFile,"<th bgcolor=" + Chr(34) + RowColor2 + Chr(34) + ">")
        EndIf 
      Else
        WriteStringN(nFile,"<th>")  
      EndIf
    EndIf
    WriteStringN(nFile,HTMLcomment("loop for column cells"))
    For nc = 0 To ArraySize(arr(),1)
      line = "<td padding=" + Str(Padding)
      If class
        line = line + " class=" + #DQUOTE$ + class + #DQUOTE$
      EndIf
      If arr(nc,nr)\bgColor
        line = line + " bgcolor=" + #DQUOTE$ + arr(nc,nr)\bgColor + #DQUOTE$
      EndIf
      Select arr(nc,nr)\align
      Case #FT_Align_Center  
        line = line + " align=" + #DQUOTE$ + "center" + #DQUOTE$
      Case #FT_Align_Left  
        line = line + " align=" + #DQUOTE$ + "left" + #DQUOTE$
      Case #FT_Align_Right  
        line = line + " align=" + #DQUOTE$ + "right" + #DQUOTE$
      Default  
      EndSelect
      line = line + ">"
      If arr(nc,nr)\font Or arr(nc,nr)\fontSize Or arr(nc,nr)\fontColor
        line = line + "<font" 
        If arr(nc,nr)\font
          line = line + " " + arr(nc,nr)\font
        EndIf
        If arr(nc,nr)\fontSize
          line = line + " size=" + arr(nc,nr)\fontSize
        EndIf
        If arr(nc,nr)\fontColor
          line = line + " color=" + arr(nc,nr)\fontColor
        EndIf
        line = line + ">"
      EndIf
      line = line + arr(nc,nr)\text
      If arr(nc,nr)\font Or arr(nc,nr)\fontSize Or arr(nc,nr)\fontColor
        line = line + "</font>"
      EndIf
      line = line + "</td>"
      WriteStringN(nFile,line)
      line = ""
    Next nc
    If Not firstRowIsHdr
      WriteStringN(nFile,"</tr>")
    Else
      WriteStringN(nFile,"</th>")
    EndIf
  Next nr
  WriteStringN(nFile,"</table>")
EndProcedure
-Jon

Fedora user
But I work with Win7
Post Reply