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
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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 30 2025 21:45:12
Script start
Timing: Jan 30 2025 21:45:12
Module start 'layout'
Timing: Jan 30 2025 21:45:12
Module start 'content'
Timing: Jan 30 2025 21:45:12
Module end 'content'
Timing: Jan 30 2025 21:45:12
Script end

Main resources:

Total runtime0.0218 sec
Peak memory usage6,144.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0055 588.1250151.2109
Module start 'layout' 0.00550.0042 739.3359220.6875
Module start 'content' 0.00970.0106 960.02341,009.8984
Module end 'content' 0.02030.0015 1,969.921941.9922
Script end 0.0218  2,011.9141 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002611.8118140.0002
Check MTime0.00104.6143140.0001
Mysql Total
Database connection0.00062.764410.0006
Mysqli_queries0.003716.790530.0012
Looping result0.00000.050210.0000
Template Total0.00115.010.0011
Template load0.00083.740110.0008
Template processing0.00031.231110.0003
Override
Cache load0.00062.751310.0006
General
dbfile0.00031.258380.0000
String conversion0.00000.031640.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs