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

4
  • This looks like serialized data. unserialize() should be the ticket.
    – Pekka
    Commented Feb 14, 2012 at 11:06
  • Are you shure? because there are no fields like: alias in it ....
    – helle
    Commented Feb 14, 2012 at 11:07
  • ahhh, I see! Then Contao is probably doing some additional magic. No idea what to do then, sorry.
    – Pekka
    Commented Feb 14, 2012 at 11:11
  • I should change the title to: What magic does f***ing contao? :D
    – helle
    Commented Feb 14, 2012 at 11:15

1 Answer 1

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.

4
  • So I will get all the fields (title, alias) when I use a User-unserialize method? Can you give some example?
    – helle
    Commented Feb 14, 2012 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? Commented Feb 14, 2012 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
    Commented Feb 14, 2012 at 14:34
  • If you're using linkCatalog[/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... Commented Feb 14, 2012 at 15:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.