|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
geoB Smarty Regular
Joined: 09 Sep 2010 Posts: 46
|
Posted: Thu Sep 09, 2010 11:50 pm Post subject: Table columns sorted alphabetically by column (not row) |
|
|
I'm a recent convert to smarty, so please forgive failures in expected comments and documentation. Here is a modifier plugin to sort an array by key values and display them sorted alphabetically vertically. For example, something that looks like this:
a d g
b e h
c f i
Code: | <?php
function smarty_modifier_alpha_column_sort($array,$cols) {
//sort array for populating a table with $cols columns, successive columns sorted alphabetically
if (!is_array($array)) {
return false;
}
ksort($array);
$n = count($array);
$rows = floor($n/$cols);
if ($n % $cols) {
$rows++;
}
for ($row = 0; $row <= $rows - 1; $row++) {
$row_array = array();
for ($col=0; $col <= $cols -1; $col++){
$col_array = array_slice($array, $row + $col*$rows, 1, true);
$row_array = $row_array + $col_array;
}
$output[$row] =$row_array ;
}
return $output;
}
|
and can be called in a template as in
Code: | {assign var='alpha' value=$ColArray|@alpha_column_sort:2}
<table>
{foreach name=outer from=$alpha item=row}
<tr>
{foreach key=k item=v from=$row}
<td>{$k}</td><td><input name="{$v}" type="checkbox" value="Yes"> </td>
{/foreach}
</tr>
{/foreach}
</table>
|
Hope someone else finds this useful!
George |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Fri Sep 10, 2010 9:50 am Post subject: |
|
|
Many thanx - will check it. |
|
Back to top |
|
geoB Smarty Regular
Joined: 09 Sep 2010 Posts: 46
|
Posted: Fri Sep 10, 2010 1:08 pm Post subject: Sorting on value instead of key |
|
|
As it turns out, in my application I need to sort by value and not key. (This is more a function of the mysql result set and how the array is created.) To sort by value, replace the ksort($array) with asort($array) and transpose the {$k} and {$v} in the template.
g |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|