0

I wrote a script which exports an contao-catalog item as PDF if the user saves the entry in the backend-view.

my problem is, that in the templates there are codes like this, if I print out i.e. the variable $entry['data']['link_img']['ref'].:

a:3:{i:0;s:2:"14";i:1;s:2:"15";i:2;s:2:"13";} (a serialized Array, which does not match the foreach specifications)

The php code looks like this:

<?php foreach ($entry['data']['link_img']['ref'] as $link_img):?>
    <? print($link_img); ?>
        <a href="werke-detail/items/<?php echo $link_img['alias']; ?>.html">
    <?php echo $link_img['title']; ?> </a><br />
<?php endforeach; ?>

when contao parses the template, this works, if I get the data, it doesn't. Is there a function from contao which I can use, to decode this, to get my foreach to work?

UPDATE

The big question is: Where does Contao do it's magic in the template-engine and how can I do the same?

Thanks

helle
  • 11,183
  • 9
  • 56
  • 83

1 Answers1

0

To extend on the comment given by Pekka. Contao stores certain data types as a serialized PHP array.

Usually within the Backend any field that is stored as follows:

$GLOBALS['TL_DCA']['tl_dca']['fields']['yourfield']['eval']['multiple'] = true;

Will store that data as serialized, some built in classes such as User will unserialize such data, ie if you're using $this->User .

Also, note that all serialized arrays are stored in BLOB types in the database, so database.sql for a given module can give you clues about if something will be in that format.

The serialize/unserialize functions are stored in system/functions.php which contains some other useful PHP functions.

Peter Brooks
  • 1,170
  • 9
  • 23
  • So I will get all the fields (title, alias) when I use a User-unserialize method? Can you give some example? – helle Feb 14 '12 at 14:24
  • That array you've specified above will give array(14,15,13) which by looking at the names to be some form of references. Are you after the image title and alias in the template? – Peter Brooks Feb 14 '12 at 14:32
  • Yes I know. I also know what `serialize` and `unserialies` does. The thing is, as you can read in the comments: where does contao do it's magic, and how can I also do it? – helle Feb 14 '12 at 14:34
  • If you're using [link](http://www.contao.org/en/extension-list/view/catalog.20000004.en.html)Catalog[/link] then it's not mainted by the Contao team but by three other developers. Can you please run down what procedure the script you've written is doing to access the data. Ie using inbuilt tl_catalog classes... – Peter Brooks Feb 14 '12 at 15:02