/ Published in: Smarty
How to sort array in smarty template?
>> Create sort modifier file and save it in smarty / plugins with
modifier.sortby.php name
use this modifier in smarty template like...
{foreach item=item key=key from=$users|@sortby:"firstname"}
{$item.id}-{$item.firstname}
{/foreach}
>> Create sort modifier file and save it in smarty / plugins with
modifier.sortby.php name
use this modifier in smarty template like...
{foreach item=item key=key from=$users|@sortby:"firstname"}
{$item.id}-{$item.firstname}
{/foreach}
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php function array_sort_by_fields(&$data, $sortby){ { $code = "\$c=0;"; { $d = '1'; { $d = '-1'; } { $code .= "if ( ( \$c = (\$a['$key'] - \$b['$key'])) != 0 ) return $d * \$c;\n"; } else { $code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return $d * \$c;\n"; } } $code .= 'return $c;'; } else { $sort_func = $sort_funcs[$sortby]; } } # # Modifier: sortby - allows arrays of named arrays to be sorted by a given field # function smarty_modifier_sortby($arrData,$sortfields) { array_sort_by_fields($arrData,$sortfields); return $arrData; } ?>
URL: http://www.smarty.net/forums/viewtopic.php?t=1079&sid=044cd556ed67f6ee4ed47e7cdeccd3ca