|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
spiderkds Smarty n00b
Joined: 22 Jan 2018 Posts: 3
|
Posted: Mon Jan 22, 2018 11:30 pm Post subject: object sorting problem |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Jan 23, 2018 12:55 pm Post subject: |
|
|
Sort the objectes where you are getting them from. |
|
Back to top |
|
spiderkds Smarty n00b
Joined: 22 Jan 2018 Posts: 3
|
Posted: Tue Jan 23, 2018 11:07 pm Post subject: |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Tue Jan 23, 2018 11:46 pm Post subject: |
|
|
Can we know where @sortby comes from? |
|
Back to top |
|
spiderkds Smarty n00b
Joined: 22 Jan 2018 Posts: 3
|
Posted: Tue Jan 23, 2018 11:56 pm Post subject: |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Jan 24, 2018 12:09 am Post subject: |
|
|
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 |
|
|
|
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
|