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

object sorting problem

 
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
spiderkds
Smarty n00b


Joined: 22 Jan 2018
Posts: 3

PostPosted: Mon Jan 22, 2018 11:30 pm    Post subject: object sorting problem Reply with quote

Hi guys,



I have a question about pdf generating. Let's say I have a object, it have 3 attributes, and the are:

"name", "size" and "startdate", and I want all the objects to sorted by "start date."

So for now, no matter how I change the code in smarty template, The order of the items in pdf files are sorted by the names alphabetically. They will be like this:



apple | big | 2/1/2018

beer | small | 1/1/2018

cat | big | 3/1/2018

So I made following change:



{foreach from=$objects item="object"}

I tried sorting it by adding the modifier as follow:



{foreach $objects|@sortby:"startdate" as $object}



Sadly, it doesn't work.



So can anyone please tell me how could I do to make "startdate" as default highest priority to sort all the objects.

Thank you very much.



Regards

Zhuo
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Jan 23, 2018 12:55 pm    Post subject: Reply with quote

Sort the objectes where you are getting them from.
Back to top
View user's profile Send private message
spiderkds
Smarty n00b


Joined: 22 Jan 2018
Posts: 3

PostPosted: Tue Jan 23, 2018 11:07 pm    Post subject: Reply with quote

AnrDaemon wrote:
Sort the objectes where you are getting them from.


I'm using sugarCRM PDF manager, when I try to generate the PDF, I want all the items to list base on their start date.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Tue Jan 23, 2018 11:46 pm    Post subject: Reply with quote

Can we know where @sortby comes from?
Back to top
View user's profile Send private message
spiderkds
Smarty n00b


Joined: 22 Jan 2018
Posts: 3

PostPosted: Tue Jan 23, 2018 11:56 pm    Post subject: Reply with quote

bsmither wrote:
Can we know where @sortby comes from?


I got the code from here:

https://www.smarty.net/forums/viewtopic.php?t=1079&postdays=0&postorder=asc&start=0

the @sortby works fine, but when I try to sort by date time, it only sortby the day date, for example:

01/02/2017
02/01/2016
03/08/2018

but what I'm tring to get is:

02/01/2016
01/02/2017
03/08/2018

I'm really new to smarty, so I don't know how to handle the date time. Please help me and leave some hits or idea how to achieve this.

Thanks!
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Wed Jan 24, 2018 12:09 am    Post subject: Reply with quote

What you have is a string, of which you are sorting on the alphanumerical order of a string.

If that's all you have, then use a modifier to convert it to a unix time integer, sort on that, then convert back to DD/MM/YYYY.

You may need to modify the modifier to verify that the field to be sorted on looks like a date that can be processed by PHP's strtotime(). If yes, change the array contents. If not, proceed as normal.

Or maybe add a second parameter to indicate what kind of field this is if it is to be treated in any special way.
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
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