PHP Helper Source Code
<?php if (!function_exists('displayTableColumns')) { /** * Takes a given array and creates (multiple) vertical columns of a single field * * @param array $arry An single dimension array to display in columns * @param integer $numberofColumns The number of columns to display (default 1). * @return string The sanitized string. */ function displayTableColumns($arry, $numberOfColumns = 1) { $x = 0; $c = 0; $num_rows = count($arry); $o = '<tbody><tr><td>'; foreach ($arry as $row) { $o .= $row; $c++; $x++; /* block an empty <td></td> at the end of the run */ if ($c < $num_rows) { /* make a new colum? */ if ($x == (ceil($num_rows / $numberOfColumns))) { $o .= ' </td><td>'; $x = 0; } else { /* create a new "row" within the current column */ $o .= '<br>'; } } } $o .= '</td></tr></tbody>'; return $o; } }
Example Snippet with an Array
<?php if (!function_exists('displayTableColumns')) { /** * Takes a given array and creates (multiple) vertical columns of a single field * * @param array $arry An single dimension array to display in columns * @param integer $numberofColumns The number of columns to display (default 1). * @return string The sanitized string. */ function displayTableColumns($arry, $numberOfColumns = 1) { $x = 0; $c = 0; $num_rows = count($arry); $o = '<tbody><tr><td>'; foreach ($arry as $row) { $o .= $row; $c++; $x++; /* block an empty <td></td> at the end of the run */ if ($c < $num_rows) { /* make a new colum? */ if ($x == (ceil($num_rows / $numberOfColumns))) { $o .= ' </td><td>'; $x = 0; } else { /* create a new "row" within the current column */ $o .= '<br>'; } } } $o .= '</td></tr></tbody>'; return $o; } } $state_list = array( 'AL' => "Alabama", 'AK' => "Alaska", 'AZ' => "Arizona", 'AR' => "Arkansas", 'CA' => "California", 'CO' => "Colorado", 'CT' => "Connecticut", 'DE' => "Delaware", 'DC' => "District Of Columbia", 'FL' => "Florida", 'GA' => "Georgia", 'HI' => "Hawaii", 'ID' => "Idaho", 'IL' => "Illinois", 'IN' => "Indiana", 'IA' => "Iowa", 'KS' => "Kansas", 'KY' => "Kentucky", 'LA' => "Louisiana", 'ME' => "Maine", 'MD' => "Maryland", 'MA' => "Massachusetts", 'MI' => "Michigan", 'MN' => "Minnesota", 'MS' => "Mississippi", 'MO' => "Missouri", 'MT' => "Montana", 'NE' => "Nebraska", 'NV' => "Nevada", 'NH' => "New Hampshire", 'NJ' => "New Jersey", 'NM' => "New Mexico", 'NY' => "New York", 'NC' => "North Carolina", 'ND' => "North Dakota", 'OH' => "Ohio", 'OK' => "Oklahoma", 'OR' => "Oregon", 'PA' => "Pennsylvania", 'RI' => "Rhode Island", 'SC' => "South Carolina", 'SD' => "South Dakota", 'TN' => "Tennessee", 'TX' => "Texas", 'UT' => "Utah", 'VT' => "Vermont", 'VA' => "Virginia", 'WA' => "Washington", 'WV' => "West Virginia", 'WI' => "Wisconsin", 'WY' => "Wyoming" ); $numberOfColumns = 2; $wsw = (function_exists('displayTableColumns')) ? '<table style="width:100%">' . displayTableColumns($state_list, 3) . '</table>' : print_r($state_list); return $wsw;
Example Implementation
[[!displayTableColumns]] <table> </table>
Snippet Output:
Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware District Of Columbia Florida Georgia Hawaii Idaho Illinois Indiana Iowa Kansas | Kentucky Louisiana Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina | North Dakota Ohio Oklahoma Oregon Pennsylvania Rhode Island South Carolina South Dakota Tennessee Texas Utah Vermont Virginia Washington West Virginia Wisconsin Wyoming |
Comments
Caching: Take special notice to the fact the "helper function" is called cache. This is intentional as the function needs to located within the cached version of the current page to be used by subsequent Snippet Calls.
Output filter: As output filters work with "string" and related character types, a PHP array can not be used as the data for this implementation of a helper function, when using Custom Output Filters.
/* do not try this with array data types - it will not work */Alternative solution
If your data is located within a database table accessible with the installation of MODX Revolution, try using Rowboat. This Addon makes short work of straightforward query presentations. I have even used it in conjunction with other Addons in Ajax scenarios, though I did notice a performance hit and decided to implement an internal solution within my PHP classes.
Rowboat does not currently function on non database table arrays, nor does it handle IN or relational queries very well - if at all. What it does extremely efficient, is to quickly access a database table and inject the result set into a template for display on the site.