Forums / Developer / Two fetches, one order

Two fetches, one order

Author Message

Lo' F.

Friday 03 December 2010 10:43:48 am

Hi!

I was wondering...

I have two fetch statements retrieving objects of a specific class.

The first fetches a number of objects (10) having a certain attribute set to true.

The second one fetches the objects only if the first one gets less then the limit set to 10.

Something like this...

{def $sel_items=fetch('content', 'tree', hash('parent_node_id', 2, 'sort_by', array('attribute', false(), 'article/publish_date'), 'class_filter_type', 'include', 'class_filter_array', array('article'), 'limit', 10, 'attribute_filter', array(array('article/highlight', '=', 1))))}



{def $counter=0}
{foreach $sel_items as $sel_item}
{set $counter= inc($counter)}
  ...
{/foreach}

{def $limit = 10|sub($counter) }
{if $limit|gt(0)}
{def $fill_items=fetch('content', 'tree', hash('parent_node_id', 2, 'sort_by', array('attribute', false(), 'article/publish_date'), 'class_filter_type', 'include', 'class_filter_array', array('article'), 'limit', $limit, 'attribute_filter', array(array('article/highlight', '=', 0))))}



{foreach $fill_items as $fill_item}
  ...
{/foreach}

I need to sort them by their publish date, but in this way I get two separated lists sorted by date.

e.g. 2.12.2010 - 30.11.2010 - 28.11.2010 (1st fetch) - 1.12.2010 - 29.11.2010 - 25.11.2010 - ... (2nd fetch)

I can't figure out how to list these two giving a unique discending order...

What can you suggest?

loredanaebook.it

Jean-Luc Chassaing

Friday 03 December 2010 11:57:08 pm

Hello,

Why don't you make one single fetch using a double sort array so you can sort them by your attribute "article/highlight" and then by publish date with a limit of 10. So that you'll first have the highlighted nodes listed by publish date and then the others..

Lo' F.

Saturday 04 December 2010 6:15:19 am

"

Why don't you make one single fetch using a double sort array so you can sort them by your attribute "article/highlight" and then by publish date with a limit of 10. So that you'll first have the highlighted nodes listed by publish date and then the others..

"

Hi Jean-Luc, thanks for your reply. Surely a neater way ..

{def $items=fetch('content', 'tree', hash('parent_node_id', 2, 'sort_by', array( array( 'attribute', false(), 'article/highlight' ), array(  'attribute', false(), 'article/publish_date')), 'class_filter_type', 'include', 'class_filter_array', array('article'), 'limit', 10))}
...

But what I need to do is arranging those fetched items according to their publish date and push the highlithed ones in the list keeping just one order

In other words, instead of getting this result..

e.g. 2.12.2010 - 30.11.2010 - 28.11.2010 (the highlighted ones) - 1.12.2010 - 29.11.2010 - 25.11.2010 - ... (the others)

I should get..

2.12.2010 (highlighted) - 1.12.2010 - 30.11.2010 (highlighted) - 29.11.2010 - 28.11.2010 (highlighted) - 25.11.2010 - ...

Well, someone may say "then why don't you just sort by date without using any attribute_filter?!"

The thing is that if the 10th item is dated, let's say, 30.7.2010, and there's a need to highlight one dated 1.7.2010, this one should be forced to be the last of the list of 10 items and get the place of 30.7.2010...

loredanaebook.it

Jean-Luc Chassaing

Sunday 05 December 2010 12:38:34 am

Ok,

So let me just try to turn that an other way... you want to sort your items from the oldest one to the soonest and I suppose that if two of them have the same publish date, the highlighted one should come first.

But if the 10th is not highlighted and there is a sooner one highlighted, you want it to take the 10th rank.

As you said, why don't you just sort them by publish date and highlighted but the other way from the sonest to the oldest.... And if you want them to apear in the oder way just display the result array from the last to the first one.

Lo' F.

Sunday 05 December 2010 8:05:48 am

"

So let me just try to turn that an other way... you want to sort your items from the oldest one to the soonest and I suppose that if two of them have the same publish date, the highlighted one should come first.

But if the 10th is not highlighted and there is a sooner one highlighted, you want it to take the 10th rank.

"

..Precisely!

But by sorting them this way I still get the two sets of dates as two separated blocks (when the first ends the other set starts), while I need to force the highlighted items inside the list of ten (pushing the others away if there's not space for them) but always keeping one sequence of date from the latest to the oldest...

How can I sort the result array based on this logic? Do I need to make something up inside the foreach before I can display them?

loredanaebook.it

Jean-Luc Chassaing

Sunday 05 December 2010 1:21:14 pm

I think you should make your on tool to solv your problem. Otherwise you'll have to make to much "php like" data manipulation in your template.

To do so, you can have a look to this tutorial to make your on fetch function : http://share.ez.no/learn/ez-publish/understanding-and-developing-fetch-functions .

And other way would be to make a template operator.

Lo' F.

Monday 06 December 2010 10:40:32 am

Thanks!

I will look into it and give a try to make a custom fetch or a template operator.

Hope to come back soon with a nice solution!

loredanaebook.it