Forums / Setup & design / Process matrix array

Process matrix array

Author Message

paul bolger

Friday 02 May 2008 8:49:05 pm

Can anyone fill me in on a neat way to do this?

I'm generating an xml playlist from a matrix. The matrix data looks like this:

>0 string 'clip one'
>1 string 'http://test.com/one.mp4'
>2 string 'http://test.com/one.flv'
>3 string 'http://test.com/one.jpg'
>4 string 'clip two'
>5 string 'http://test.com/two.mp4'
>6 string 'http://test.com/two.flv'
>7 string 'http://test.com/two.jpg'
>8 string 'clip three'
>9 string 'http://test.com/three.mp4'
>10 string 'http://test.com/three.flv'
>11 string 'http://test.com/three.jpg'

and I need to turn it into this:

<item>
<title>clip one</title>
<location>http://test.com/one.mp4</location>
<fallback>http://test.com/one.flv</fallback>
<image>http://test.com/one.jpg</image>
</item>
<item>
<title>clip two</title>
<location>http://test.com/two.mp4</location>
<fallback>http://test.com/two.flv</fallback>
<image>http://test.com/two.jpg</image>
etc etc

Thanks.

Paul Bolger

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
[email protected] | 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
[email protected] | 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
[email protected] | 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