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

Sort By

 
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
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Wed Aug 10, 2011 7:56 pm    Post subject: Sort By Reply with quote

Hi,

I am trying to get the following smarty Sort function working.

Code:
 
#
# sorts an array of named arrays by the supplied fields
#   code by dholmes at jccc d0t net

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 ( \$a['$key'] > \$b['$key']) return $d * 1;\n";
$code .= "if ( \$a['$key'] < \$b['$key']) return $d * -1;\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" );   


I have it successfully working in another website but I cannot get it going in this one. I am getting the following error message.

Fatal error: Cannot use object of type stdClass as array in
/home/hfadmin/public_html/lib/smarty/plugins/modifier.sortby.php(33) : runtime-created function on line 1

This is my array,


Code:
stdClass Object
(
    [id] => 49
    [product_name] => 145634
    [details] =>
    [price] => 0
    [create_date] => 2011-08-08 14:58:55
    [modified_date] => 2011-08-08 14:58:55
    [taxable] => 0
    [status] => published
    [weight] => 0
    [sku] =>
    [alias] => 145634
    [detail_url] =></a>
    [file_location] =>
    [hierarchy_id] => -1
    [breadcrumb] =>
    [fields] => Array
        (
            [course_name] => stdClass Object
                (
                    [id] => 18
                    [name] => course_name
                    [prompt] => Course Name*
                    [type] => textbox
                    [options] => Array
                        (
                            [] =>
                        )
 
                    [max_length] => 400
                )
1


I guess it is because I have a stdClass array. Is there anyway I can get the sort working using a stdClass array?

Thanks

Chris
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Wed Aug 10, 2011 8:39 pm    Post subject: Re: Sort By Reply with quote

stdClass is an object, not an array. I don't think objects are sortable.


edit: i take that back...

stackoverflow . com/questions/124266/sort-object-in-php
Back to top
View user's profile Send private message
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Wed Aug 10, 2011 8:57 pm    Post subject: Reply with quote

Hi,

Thanks for the reply. I don't suppose you would be able to help me figure out how to create a sort using the field 'course_name' from my object using the methods listed in the link you sent?

Any help would be greatly appreciated.

Thanks

Chris
Back to top
View user's profile Send private message
Mahindra
Smarty Rookie


Joined: 25 Aug 2011
Posts: 6

PostPosted: Tue Oct 04, 2011 10:45 pm    Post subject: Sort By Reply with quote

Hi.

Use in TfcCube before open cube:
// base fields feature
procedure SetBaseFieldAField, ABaseField: String;
function HaveBaseFieldAField: String: Boolean;
procedure ClearBaseFields;

For example:
Field1 - string field to display
Field2 - numeric field to sort
You need:
fcCube.SetBaseFieldField1, Field2;

Oleg Pryalkov
Back to top
View user's profile Send private message Send e-mail AIM Address
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