Page 1 of 1
Lise sort elements by year and by date and show year
Posted: Wed Apr 19, 2023 4:28 pm
by pierrepercee
In Lise I would like to have this presentation:
2023
element
element
element
2020
element
element
2018
element
element
element
element
element
element
I use this code in my summary template:
Code: Select all
{assign var="yet" value=$smarty.now|date_format:"%Y"}
{foreach from=$items item=item}
{if $item@last}
{assign var="mylast" value=$item->fielddefs.datepubli ->value|date_format:"%Y"}
{/if}
{/foreach}
{for $tostart=$yet;$tostart>=$mylast;$tostart--}
<h4 class="myyear">Year {$tostart}</h4>
<ul>
{foreach from=$items item=item}
{if $item->fielddefs.uploadsbrak!= ''}
{if $item->fielddefs.datepubli ->value|date_format:"%Y"=={$tostart}}
<li> <strong>Number: </strong>{if $item->fielddefs.mynumb!= ''} {$item->fielddefs.mynumb->value}{/if}</li>
{/if}
{/if}
{/foreach}
</ul>
{/for}
{/if}
It almost works but when there is no element for a year it still displays the year. If someone has an idea...
Re: Lise sort elements by year and by date and show year
Posted: Wed Apr 19, 2023 4:43 pm
by velden
In this scenario (and I think it doesn't really sort by the whole date, just the year) you will need to use a little more logic:
Code: Select all
{for $tostart=$yet;$tostart>=$mylast;$tostart--}
{$foundItem=false}
{foreach from=$items item=item}
{if $item->fielddefs.uploadsbrak!= ''}
{if !$foundItem}
<h4 class="myyear">Year {$tostart}</h4>
<ul>
{$foundItem=true}
{/if}
{if $item->fielddefs.datepubli ->value|date_format:"%Y"=={$tostart}}
<li> <strong>Number: </strong> {$item->fielddefs.mynumb->value}</li>
{/if}
{/if}
{/foreach}
{if $foundItem}</ul>{/if}
{/for}
Re: Lise sort elements by year and by date and show year
Posted: Wed Apr 19, 2023 5:00 pm
by velden
BTW:
You may want to check this LISE help:
(optional) orderby = item_position - You can order by any of the following columns: item_id, item_title, item_position, item_created, category_id, category_name, category_position, category_hierarchy, rand and also by custom fields with custom_* (* would be the field definition alias).
That might save a few loops. Other logic will be required then.
Re: Lise sort elements by year and by date and show year
Posted: Wed Apr 19, 2023 5:36 pm
by pierrepercee
Thanks Velden,
I already use orderby custom field in my call (datepubli). It's why items are sorted by whole date i think inside each year.
I don't know how to use other logic in this context by decreasing the number of loops. This is important because I will have several thousand items here.
Each item has a title (120 characters max), an alpha numeric reference (10 characters), a publication date and an uploaded file.
Very few fields in fact...
If I can save a little resource with a better thought code, I'm interested

Re: Lise sort elements by year and by date and show year
Posted: Wed Apr 19, 2023 5:54 pm
by velden
If the items are sorted by date (so also by year) this should work I think (untested):
Code: Select all
{$year=''}
{foreach from=$items item=item}
{if $year != $item->fielddefs.datepubli->value|date_format:"%Y"}
{$year == $item->fielddefs.datepubli->value|date_format:"%Y"}
{if !$item@first}</ul>{/if}
<h4 class="myyear">Year {$year}</h4>
<ul>
{/if}
<li><strong>Number: </strong> {$item->fielddefs.mynumb->value}</li>
{/foreach}
</ul>
Re: Lise sort elements by year and by date and show year
Posted: Wed Apr 19, 2023 7:04 pm
by pierrepercee
Velden,
You are the best developer of all the known and unknown universes, really.
Just a little tipo :
== becomes =
It works like a charm !
Do you think with two thousands items it coulds slow down execution on a basic shared hosting ?
Really many thanks !