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

Sorting arrays inside templates
Goto page Previous  1, 2
 
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 -> General
View previous topic :: View next topic  
Author Message
Alpha_Numeric
Smarty Rookie


Joined: 18 Aug 2008
Posts: 6

PostPosted: Mon Oct 20, 2008 9:46 pm    Post subject: Reply with quote

Celeb

Sorry for the delay in responding. I am sure I click the 'Notify me when a reply is posted' but don't seem to be seeing the notice. Perhaps my SPAM filter -I will check.

I made the change to the file. Taking out:

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


It makes no difference to the result of course.

The issue is, the array never sorts, it doesn't seem to matter what I try and place as an argument to sortby. I have error_reporting 6143 turned on and all. It doesn't throw an error, it simply doesn't sort.

Is there someway for me to provide better input in helping the forum community help me?
Back to top
View user's profile Send private message
Alpha_Numeric
Smarty Rookie


Joined: 18 Aug 2008
Posts: 6

PostPosted: Tue Oct 21, 2008 3:07 am    Post subject: Reply with quote

Celeb

Sorry for wasting your time on this one. It was a programming error, so simple I did not see it.

The function was re-ordering the array, but the code:

Code:
assign var=id value=$smarty.foreach.Tickets.index}
                        {assign var=ticket value=$Tickets.$id}


Is using the index (which remains true to the re-ordered data set) to output the array entries in their original order, not re-sorted order.

Oops.
Back to top
View user's profile Send private message
douglaskarr
Smarty n00b


Joined: 05 Jan 2010
Posts: 1

PostPosted: Tue Jan 05, 2010 4:21 pm    Post subject: Error Reply with quote

I get an error as well:
Code:
Fatal error: Cannot use object of type MDB2_BufferedResult_mysql as array in modifier.sortby.php(34) : runtime-created function on line 1
Back to top
View user's profile Send private message
fl3x7
Smarty Rookie


Joined: 12 Feb 2011
Posts: 8

PostPosted: Wed Feb 16, 2011 9:29 am    Post subject: Reply with quote

Hope someone can help. Im just trying to order a simple array. No keys specified eg

Array ( [0] => 100[1] => 50[2] => 75)

I would just like to sort the above smallest first. Normally I would use one of the php sort functions but I need to do this in the smarty template.

Ive added the plugin and have the following code so far:

Code:
 {assign var='myarray' value=','|explode:"100,50,75"}
 
 {foreach from=$myarray item=i nocache}
     {$i}
 {/foreach}


Everytime i add the sortby like this:
Code:

{foreach from=$myarray|@sortby:"#" item=i nocache}


I get the error:

Fatal error: Call to a member function register_modifier() on a non-object in xxxx\smarty\libs\plugins\modifier.sortby.php on line 51

Im pretty new to smarty so any help would be appreciated Smile

Thanks a lot
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Feb 16, 2011 2:56 pm    Post subject: Reply with quote

What does modifier.sortby.php look like?
Back to top
View user's profile Send private message Visit poster's website
fl3x7
Smarty Rookie


Joined: 12 Feb 2011
Posts: 8

PostPosted: Wed Feb 16, 2011 3:08 pm    Post subject: Reply with quote

thanks for your reply.

The modifier.sortby.php was the one posted by pscsuk on the first page.

However I have now just created a new simple function and all seems to be well (first time ive ever used custom functions in smarty woo)

thanks a lot Smile
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Feb 16, 2011 3:17 pm    Post subject: Reply with quote

I tried this and it worked:

Code:
{$data|@sort:$smarty.const.SORT_NUMERIC}
{foreach from=$data item="foo"}
  {$foo}
{/foreach}


the php "sort" function sorts an array in-place, so this won't work in-line with the foreach loop. you have to sort first, then loop it.
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Feb 16, 2011 3:21 pm    Post subject: Reply with quote

The originally posted modifier.sortby.php did contain an unneeded/unwanted $smarty->register_modifier( "sortby", "smarty_modifier_sortby" ); which should be removed. This was also discussed in this thread befor.
Back to top
View user's profile Send private message
jaze22
Smarty Rookie


Joined: 06 Jan 2010
Posts: 6

PostPosted: Fri Mar 29, 2013 3:50 pm    Post subject: Reply with quote

Who can modify the sortby plugin to work with a timestamp?

order by date sortby views

Date in the array is currently a unix timestamp and it won't sort because of it.
Back to top
View user's profile Send private message
programmining
Smarty Rookie


Joined: 18 Oct 2012
Posts: 6

PostPosted: Wed Jun 12, 2013 2:25 pm    Post subject: Reply with quote

Hey all. Using Smarty-3.1.13 - does the sortby modifier still work in this version - or - was this script update included for it?

Update to the link previously posted on page one about variable modifiers (notice how sort doesn't exist):

http://www.smarty.net/docsv2/en/language.modifiers

Thanks,
pmmg
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Jun 12, 2013 6:51 pm    Post subject: Reply with quote

The sortby modifier discussed here is not included in the Smarty distribution, it's a custom implementation.
It should work also with 3.1 but it's not tested.
Back to top
View user's profile Send private message
programmining
Smarty Rookie


Joined: 18 Oct 2012
Posts: 6

PostPosted: Wed Jun 12, 2013 7:23 pm    Post subject: Reply with quote

Thanks U.Tews.

For our team, our Dev's are pushing hard to keep this out of the templates. Already other projects have moved towards json & js templating systems so from a processes and technology perspective it makes sense for us to keep it out.

As much as I'd like to provide the client with a cheaper price point for this functionality by doing it on the Front End, I can't find any justifications.

Do any of you have any?

Thanks,
pmmg
Back to top
View user's profile Send private message
cole
Smarty n00b


Joined: 23 Jan 2019
Posts: 1

PostPosted: Wed Jan 23, 2019 12:50 am    Post subject: Reply with quote

Function create_function() is Deprecated in PHP 7.2

Code:
$sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);


Thanks on any help.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jan 27, 2019 6:20 am    Post subject: Reply with quote

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 -> General All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
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