From beea7f4f9eed4ff707e87bfb660711cf7c5ae138 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Tue, 16 Dec 2008 14:07:58 +0000 Subject: [PATCH] Add two new XML methods for building tables (buildTable and buildTableRow). Accepts associative arrays where the key is the row/cell id and the value is its value. Non-string keys are skipped. --- includes/Xml.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/includes/Xml.php b/includes/Xml.php index 78f958f8f1..68990d86df 100644 --- a/includes/Xml.php +++ b/includes/Xml.php @@ -660,6 +660,47 @@ class Xml { return $form; } + + /** + * Build a table of data + * @param array $rows An array of arrays of strings, each to be a row in a table + * @param array $attribs Attributes to apply to the table tag [optional] + * @param array $headers An array of strings to use as table headers [optional] + * @return string + */ + public static function buildTable( $rows, $attribs = array(), $headers = null ) { + $s = Xml::openElement( 'table', $attribs ); + if ( is_array( $headers ) ) { + foreach( $headers as $id => $header ) { + $attribs = array(); + if ( is_string( $id ) ) $attribs['id'] = $id; + $s .= Xml::element( 'th', $attribs, $header ); + } + } + foreach( $rows as $id => $row ) { + $attribs = array(); + if ( is_string( $id ) ) $attribs['id'] = $id; + $s .= Xml::buildTableRow( $attribs, $row ); + } + $s .= Xml::closeElement( 'table' ); + return $s; + } + + /** + * Build a row for a table + * @param array $cells An array of strings to put in + * @return string + */ + public static function buildTableRow( $attribs, $cells ) { + $s = Xml::openElement( 'tr', $attribs ); + foreach( $cells as $id => $cell ) { + $attribs = array(); + if ( is_string( $id ) ) $attribs['id'] = $id; + $s .= Xml::element( 'td', $attribs, $cell ); + } + $s .= Xml::closeElement( 'tr' ); + return $s; + } } class XmlSelect { -- 2.20.1