Jump to content

2d Array to HTML table

Recommended Posts

I am trying to take an array and feed this into the body of an email which I am building up ready to be sent out. I have seen some examples in the forum but is usually taking html to array etc the other way round.  

I have the email includes I need to generate the email just struggling to see an easy way of generating a table from an array.

Any ideas or examples of anyone doing something similar.  I will start working on writing the html code from scratch if there is noting out in the forum.


Many thanks


Share this post

Link to post
Share on other sites

Just loop through your array (use a for loop within a for loop) and construct a string.  Add the prefix of the <tr> prior to entering the inner for loop, </tr> after exiting the inner for loop, <td> before outputting the inner for loop...etc etc...outside of both loops, add in the <table> </table>.  Give it a go.  You can get tricky and add a header <th> on the first inner loop.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post

Link to post
Share on other sites

I wrote this just for fun.

#include <Array.au3>

Local $aArray[4][4]

For $f = 0 To 3
    For $c = 0 To 3
        $aArray[$f][$c] = "$aArray[" & $f & "][" & $c & "]"


Local $sHtmlTable = _CreateTableFrom2DArray($aArray)

FileWrite("TestTable.html", $sHtmlTable)


Func _CreateTableFrom2DArray($aArray)
    Local $sTable = '<table border="1">' & @CRLF
    For $f = 0 To UBound($aArray, 1) - 1
        $sTable &= '<tr>' & @CRLF
        For $c = 0 To UBound($aArray, 2) - 1
            $sTable &= '<td>'
            $sTable &= $aArray[$f][$c]
            ConsoleWrite($aArray[$f][$c] & @TAB)
            $sTable &= '</td>' & @CRLF
        $sTable &= '</tr>' & @CRLF
    $sTable &= '</table>'
    Return $sTable
EndFunc   ;==>_CreateTableFrom2DArray

I also found this over the forum 



Edited by Danyfirex

Share this post

Link to post
Share on other sites

Thanks all

jdelaney: That is exactly what i was planning to start writing if there was no now udf`s already in use for this purpose.  

Danyfirex: Impressive speed on what would have took be half a day to write something similar.


I will post back what I come up with as my final email body with decent formatting etc so the table looks a bit prettier.

Many thanks for your time and help.  




Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...