Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

How to sort items by date & calendar week

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
micab
Smarty Rookie


Joined: 29 May 2015
Posts: 6

PostPosted: Fri May 29, 2015 8:30 am    Post subject: How to sort items by date & calendar week Reply with quote

Hi smarty experts!

would highly appreciate if someone could kindly let me know how to sort and display items by date added & for each calendar week a new row.

-----------------------------------------
for eg:

items added for calendar week 20
a row with items for that week

items added for calendar week 19
a row with items for that week

items added for calendar week 18
a row with items for that week

etc..

-----------------------------------------

any ideas?

thanks in advance
Back to top
View user's profile Send private message
micab
Smarty Rookie


Joined: 29 May 2015
Posts: 6

PostPosted: Tue Jun 02, 2015 10:47 am    Post subject: Reply with quote

i tried the following but with no success. can someone please help me out. thanks in advance.

{foreach item=table_data from=$products|@sortby:"-#date_added"}
<div class="{cycle values="odd"}">{$table_data.date_added|date_format:"%V"}</div>
{foreach from=$table_data.date_added|date_format:"%V" item=stat}
<div class="{cycle values="even"}">
{$stat}

{/foreach}
{/foreach}</div>
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1646

PostPosted: Tue Jun 02, 2015 11:01 am    Post subject: Reply with quote

Do your sorting in your application code. Template code should only display them.
Back to top
View user's profile Send private message
micab
Smarty Rookie


Joined: 29 May 2015
Posts: 6

PostPosted: Tue Jun 02, 2015 11:22 am    Post subject: Reply with quote

the issue that im having is to have it displayed in the right order. the result that im getting is:

Week 22
item 1 - Date added: 28.05.2015
Week 22
item 2 - Date added: 28.05.2015
Week 22
item 3 - Date added: 28.05.2015
Week 16
item 1 - Date added: 15.04.2015
Week 12
item 1 - Date added: 18.03.2015
Week 12
item 2 - Date added: 18.03.2015

etc.

but i wish to have it displayed as:

Week 22
item 1 - Date added: 28.05.2015
item 2 - Date added: 28.05.2015
item 3 - Date added: 28.05.2015

Week 16
item 1 - Date added: 15.04.2015

Week 12
item 1 - Date added: 18.03.2015
item 2 - Date added: 18.03.2015

Confused
Back to top
View user's profile Send private message
adn
Smarty Regular


Joined: 31 May 2011
Posts: 91

PostPosted: Tue Jun 02, 2015 2:19 pm    Post subject: Reply with quote

Hi micab,

I have this kind of thing in some tpl and I do something like that with only one loop. Not tested


Code:
{$week=''}
{foreach item=table_data from=$products|@sortby:"-#date_added"}
   {if $week != $table_data.date_added|date_format:"%V"}
       <div class="{cycle values="odd"}">{$table_data.date_added|date_format:"%V"}</div>
       {$week=$table_data.date_added|date_format:"%V"}
   {/if}
   <div class="{cycle values="even"}">{$stat}</div>
{/foreach}

_________________
http://www.letsGarden.info
Back to top
View user's profile Send private message Visit poster's website
micab
Smarty Rookie


Joined: 29 May 2015
Posts: 6

PostPosted: Tue Jun 02, 2015 3:07 pm    Post subject: Reply with quote

thanks adn, it doesn t seem to be working.
Back to top
View user's profile Send private message
adn
Smarty Regular


Joined: 31 May 2011
Posts: 91

PostPosted: Tue Jun 02, 2015 3:46 pm    Post subject: Reply with quote

What is your result now?
_________________
http://www.letsGarden.info
Back to top
View user's profile Send private message Visit poster's website
micab
Smarty Rookie


Joined: 29 May 2015
Posts: 6

PostPosted: Tue Jun 02, 2015 3:48 pm    Post subject: Reply with quote

no output, just a blank page.
Back to top
View user's profile Send private message
adn
Smarty Regular


Joined: 31 May 2011
Posts: 91

PostPosted: Wed Jun 03, 2015 6:52 am    Post subject: Reply with quote

At least one mistake in my code on $stat

{$week=''}
{foreach item=table_data from=$products|@sortby:"-#date_added"}
{if $week != $table_data.date_added|date_format:"%V"}
<div class="{cycle values="odd"}">{$table_data.date_added|date_format:"%V"}</div>
{$week=$table_data.date_added|date_format:"%V"}
{/if}
<div class="{cycle values="even"}">{$table_data.date_added}</div>
{/foreach}
_________________
http://www.letsGarden.info
Back to top
View user's profile Send private message Visit poster's website
micab
Smarty Rookie


Joined: 29 May 2015
Posts: 6

PostPosted: Wed Jun 03, 2015 7:00 am    Post subject: Reply with quote

Hi adn, still the same error - no output just a blank page Crying or Very sad
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1646

PostPosted: Wed Jun 03, 2015 9:00 am    Post subject: Reply with quote

Enable all error reporting.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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