Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Table columns sorted alphabetically by column (not row)

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
geoB
Smarty Regular


Joined: 09 Sep 2010
Posts: 46

PostPosted: Thu Sep 09, 2010 11:50 pm    Post subject: Table columns sorted alphabetically by column (not row) Reply with quote

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
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Fri Sep 10, 2010 9:50 am    Post subject: Reply with quote

Many thanx - will check it.
Back to top
View user's profile Send private message
geoB
Smarty Regular


Joined: 09 Sep 2010
Posts: 46

PostPosted: Fri Sep 10, 2010 1:08 pm    Post subject: Sorting on value instead of key Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP