Jump to content

Smarty Sorting of Arrays


Recommended Posts

drop the following into your

./libs/plugins/

folder as modifier.sortby.php

 

<?php
#
# sorts an array of named arrays by the supplied fields
#   code by dholmes at jccc d0t net
#   taken from http://au.php.net/function.uasort
# modified by cablehead, messju and pscs at http://www.phpinsider.com/smarty-forum

function array_sort_by_fields(&$data, $sortby){
     static $sort_funcs = array();

   if (empty($sort_funcs[$sortby]))
   {
       $code = "\$c=0;";
       foreach (split(',', $sortby) as $key)
       {
          $d = '1';
             if (substr($key, 0, 1) == '-')
             {
                $d = '-1';
                $key = substr($key, 1);
             }
             if (substr($key, 0, 1) == '#')
             {
                $key = substr($key, 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;';
       $sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);
   }
   else
   {
       $sort_func = $sort_funcs[$sortby];
   }   
   uasort($data, $sort_func);   
}

#
# 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;
}

$smarty->register_modifier( "sortby", "smarty_modifier_sortby" );   
?>

 

then you can modify and array at output time to a new sort order:

 

e.g in ./admin/templates/clientsummary.tpl

{foreach key=num from=$productsummary|@sortby:"dpackage,domain" item=product}

 

will sort the client products in a more friendly package, name order :)

 

see http://www.phpinsider.com/smarty-forum/viewtopic.php?t=1079

Link to comment
Share on other sites

  • 4 months later...

Hi Rob

 

Should this work 'as-is' as I've just had a web dev upload the file to the dir suggested and he's ammended the clientarea file to sort using:

{foreach key=num from=$services|@sortby:"Active" item=service}

 

However the clientarea now gives the error:

Fatal error: Template error: [in <theme>/clientareaproducts.tpl line 21]: [plugin] modifier 'sortby' is not implemented (core.load_plugins.php, line 118) in <url to whmcs clinet portal>/includes/smarty/Smarty.class.php on line 1095

 

Any suggestions?

Link to comment
Share on other sites

  • 3 weeks later...

Look at the error... the modifier is not being loaded

 

If you had put the file into the libs/plugins directory as per post #1and you are now using V4.1 then you will need to move the modifier file to includes/smarty/plugins directory

 

The libs directory is no longer used for V4

Link to comment
Share on other sites

Look at the error... the modifier is not being loaded

 

If you had put the file into the libs/plugins directory as per post #1and you are now using V4.1 then you will need to move the modifier file to includes/smarty/plugins directory

 

The libs directory is no longer used for V4

 

Your right, as I would think I am as well. The plugin needs to be uploaded to the proper folder, and he needs to reference the field name to sort by, not the content of a particular field.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated