Share » Forums » Setup & design » Process matrix array

Process matrix array

Process matrix array

Friday 02 May 2008 8:49:05 pm - 5 replies

Author Message

Maxime Thomas

Saturday 03 May 2008 12:51:48 am

I will do this like this :

$results = array();
$results[]='<item>';

foreach ($matrix_data as $index => $row)
{

$case = $index % 4;

switch ($case)
{
case 0 :
$results[]="<title>$row</tile>";
break;
case 1:
$results[]="<location>$row</location>";
break;
case 2:
$results[]="<fallback>$row</fallback>";
break;
case 3 : 
$results[]="<image>$row</image>";
break;
}

}

$results[]='</item>';

Maxime Thomas
maxime.thomas@wascou.org | www.wascou.org | http://twitter.com/wascou

Company Blog : http://www.wascou.org/eng/Company/Blog
Technical Blog : http://share.ez.no/blogs/maxime-thomas

paul bolger

Saturday 10 May 2008 5:47:07 am

I must admit that I'm having real problems getting this to work. I'm a little hazy on some of the template syntax used here (and the documentation doesn't seem to explain it).

What does adding empty square brackets to a variable do?

$results[]

And what does a

&

do?

regards

Paul Bolger

Maxime Thomas

Monday 12 May 2008 10:39:30 am

Hi Paul,

I've written the PHP version of the process, in case you were in a custom module/function.
By the way, you can adapt to the template version :

{def $results = '<item>'}

{foreach ($matrix_data as $index => $row)}

{def $case = $index|mod(4)}

{switch match=$case}
{case match=0}
{set $results=$results|append("<title>$row</title>")}
{/case}
{case match=1}
{set $results=$results|append("<location>$row</location>")}
{/case}

{case match=2}
{set $results=$results|append("<fallback>$row</fallback>")}
{/case}

{case match=3}
{set $results=$results|append("<image>$row</image>")}
{/case}

{/switch}

{/foreach}

{set $results=$results|append('</item>')}

{$results}

Is it clearer ?

Maxime Thomas
maxime.thomas@wascou.org | www.wascou.org | http://twitter.com/wascou

Company Blog : http://www.wascou.org/eng/Company/Blog
Technical Blog : http://share.ez.no/blogs/maxime-thomas

paul bolger

Wednesday 14 May 2008 5:18:15 am

Thanks Maxime

That looks a lot more like what I understand. "Learn PHP" is unfortunately still on my todo list...

As a matter of interest I managed to make the template work using the sequence array method too. I'd be interested to know if you think this is worse, or more inefficient, than your method. I might put both into the documentation as comments in the Matrix section - save myself a lot of time when I need to do this again!

{def   $objects = $attribute.content.cells}
<title>{$attribute.object.name}</title>
<trackList>
{foreach $objects as $object sequence array( 'title', 'location' , 'fallback' , 'image') as $element}
{if eq( $element, 'title')}
<track>
{/if}
{if eq( $element, 'fallback')}
<meta rel='{$element}'>{$object}</meta>
{else}
<{$element}>{$object}</{$element}>
{/if}
{if eq( $element, 'location')}
<meta rel='type'>flv</meta>
{elseif eq( $element, 'image')}
</track>
{/if}
{/foreach}
</trackList>

Paul Bolger

Maxime Thomas

Wednesday 14 May 2008 10:40:02 pm

Hi Paul,

I guess it's the same. However, my way is closer to PHP because the sequence functionnality does not exist in PHP.

Max

Maxime Thomas
maxime.thomas@wascou.org | www.wascou.org | http://twitter.com/wascou

Company Blog : http://www.wascou.org/eng/Company/Blog
Technical Blog : http://share.ez.no/blogs/maxime-thomas

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu