I was searching for a way to output my textarea string that is separated by line break "/n" into a dynamic HTML table on an email template.
I needed this because I have users that paste lot of information into a textarea (separated by line break - and I didn't want the email to be a huge long single column list that goes on forever.
Original Output:
$this->data['simplex_alert___affected_site_list'];
Site 1
Site 2
Site 3
Site 4
Site 5
Site 6
etc...
New Output (With dynamic table) and options on how many columns you want.
Site 1 | Site 2 | Site 3
Site 4 | Site 4 | Site6
etc...
I did not create this - but it did take a while to find...so I will take credit for finding it I also added the explode piece into the original contributors code. Obviously a big thanks to the original contributor.
Contributor Site
I needed this because I have users that paste lot of information into a textarea (separated by line break - and I didn't want the email to be a huge long single column list that goes on forever.
Original Output:
$this->data['simplex_alert___affected_site_list'];
Site 1
Site 2
Site 3
Site 4
Site 5
Site 6
etc...
New Output (With dynamic table) and options on how many columns you want.
Site 1 | Site 2 | Site 3
Site 4 | Site 4 | Site6
etc...
I did not create this - but it did take a while to find...so I will take credit for finding it I also added the explode piece into the original contributors code. Obviously a big thanks to the original contributor.
Contributor Site
PHP:
// Numeric array with data that will be displayed in HTML table
//Fabrik element
$str = $this->data['simplex_alert___affected_site_list'];
$aray1 = explode("\n", $str);
$aray = array_unique($aray1);
$nr_elm = count($aray); // gets number of elements in $aray
// Create the beginning of HTML table, and of the first row
$html_table = '<table border="1 cellspacing="0" cellpadding="2""><tr>';
$nr_col = 6; // Sets the number of columns
// If the array has elements
if ($nr_elm > 0) {
// Traverse the array with FOR
for($i=0; $i<$nr_elm; $i++) {
$html_table .= '<td>' .$aray[$i]. '</td>'; // adds the value in column in table
// If the number of columns is completed for a row (rest of division of ($i + 1) to $nr_col is 0)
// Closes the current row, and begins another row
$col_to_add = ($i+1) % $nr_col;
if($col_to_add == 0) { $html_table .= '</tr><tr>'; }
}
// Adds empty column if the current row is not completed
if($col_to_add != 0) $html_table .= '<td colspan="'. ($nr_col - $col_to_add). '"> </td>';
}
$html_table .= '</tr></table>'; // ends the last row, and the table
// Delete posible empty row (<tr></tr>) which cand be created after last column
$html_table = str_replace('<tr></tr>', '', $html_table);
echo $html_table; // display the HTML table
Last edited: