Revision [8908]
This is an old revision of ArrayToColumns made by DarTar on 2005-06-07 10:45:11.
Generating Columns from an Array
See also:
This page presents new code for the WikkaCore Wikka Core to generate columns of items from an array of items to be presented - without using a table for layout.Why?
In many cases we get an array back from some procedure and want to display the result in the form of a number of easy to read columns. The current Wikka code now does this "manually" and generates a table layout which is not only very structural code but also not very accessible. (Example: the CategoryActionInfo Category action can generate its output in "columns" but they need to be read horizontally rather than vertically - making it hard to "scan" so a particular name is hard to find - and the code generated is a layout table.)
Using a standard way to generate columns is more efficient, can ensure consistent and structural code and makes it easier to write methods that need columns of items as output.
How?
Two separate methods were written to make it easy to generate well-structured columns with 'hooks' for styling: the first does nothing but generate a list from an array (with an id); the second wraps that in a bit of code with a heading and a bit of text instead of the list if the array happens to be empty.
The Code
makeCols()
This method does the hard work of creating columnar output from an array, using floated divs instead of a layout table. It requires the makeId() method to be already present - see GenerateUniqueId.Add the following code in the //MISC section right after the makeMemberList() method (see ArrayToList for that):
makeCols()
- /**
- * Build vertically-arranged columns from an array.
- *
- * Given an array and number of columns, this method builds vertically-
- * arranged columns (meaning they are read vertically, one by one)
- * implemented as floated divs. Column width is derived from the
- * specified number of columns, implemented as an embedded style because
- * it's dynamically generated; left float is added to the style (hardcoded
- * for now but alignment could become an option later).
- *
- * @author {@link http://wikka.jsnx.com/JavaWoman JavaWoman}
- * @copyright Copyright © 2005, Marjolein Katsma
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- * @version 0.3
- *
- * @todo - make alignment configurable
- * - class as extra parameter (for each column)
- * - replace clear: both style at end with a class
- * - support for internationalization (replace hard-coded strings)
- *
- * @access public
- *
- * @param array $array required: flat array with content items; must be array but may be empty
- * @param integer $cols optional: number of columns to generate; default: 1
- * (will be cast to positive integer)
- * @return string generated divs (or empty string if array was empty)
- */
- function makeCols($array,$cols=1)
- {
- // validate/treat input
- {
- return '<p class="error">Could not generate columns: array required</p>'."\n"; # @@@ i18n
- }
- // build columns
- $width = (int) (100 / $cols);
- $lines = ($count % $cols == 0) ? (int)($count / $cols) : (int)(($count + $cols) / $cols);
- $line = 0;
- $out = '';
- // data columns
- foreach ($array as $val)
- {
- if (($line++ % $lines) == 0) $out .= "\n".' <div style="width: '.$width.'%; float: left;">'."\n";
- $out .= ' '.$val.'<br />'."\n";
- if (($line % $lines) == 0) $out .= " </div>";
- }
- if ($count > 0)
- {
- if (($line % $lines) != 0) $out .= " </div>";
- $out .= '<br style="clear: both;">'."\n";
- }
- // return result
- return $out;
- }
makeMemberCols()
This method is a wrapper around the makeCols() method: it wraps the output in a div and adds a heading or outputs a paragraph of text in case the array turns out to be empty. It's meant for cases where items are to be presented as "members" of a collection; the heading is used to indicate the number of members.As with the ArrayToList makeMemberList() method there is deliberately no 'outer' div with an id: this is so a wrapper div may be created with a single heading containing several occurrences of the output of this method. Such a wrapper should be generated in the calling code.
Add the following code in the //MISC section right after the makeCols() method:
makeMemberCols()
- /**
- * Display members of a collection in vertical columns; a heading precedes
- * the columns; alternatively output is just a paragraph of text in case the
- * array provided turns out to be empty.
- *
- * There is deliberately no 'outer' div with an id: this is so a div may be
- * created with a single heading containing several occurrences of the
- * output of this method. Such a wrapper should be generated in the calling code.
- *
- * @author {@link http://wikka.jsnx.com/JavaWoman JavaWoman}
- * @copyright Copyright © 2005, Marjolein Katsma
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- * @version 0.5
- *
- * @access public
- * @uses makeId()
- * @uses makeCols()
- *
- * @param array $array required: flat array with collection items
- * @param string $hd required: heading for the whole
- * @param string $hdid required: id for the heading
- * @param string $txtnone required: text to display when the array is empty
- * @return string div containing heading and list, or paragraph
- */
- function makeMemberCols($array,$cols,$hd,$hdid,$txtnone)
- {
- $out = '';
- if ($count > 0)
- {
- $out .= ' <h6 id="'.$this->makeId('hn',$hdid).'">'.$hd.'</h6>'."\n";
- $out .= $this->makeCols($array,$cols);
- }
- else
- {
- $out = ' <p>'.$txtnone.'</p>'."\n";
- }
- return $out;
- }
When and where to use
Status
CategoryDevelopmentCore