Yootheme Zoo - Accessing Element Data with Joomla Code

on Wednesday, 23 March 2011.

If you're looking to write some custom code with yootheme's zoo then you'll probably be wondering how to access all the data in those dynamic fields you created through their lovely admin user interface. If you look in the jos_zoo_item table in your database you'll see a column called elements. If you look at the contents you'll see that majority of your zoo items data is stored in there as XML.

If you are coding within a zoo page you can use their tutorial. However if you want a simple global function that will work anywhere in Joomla you can use the function that I've written below:

PHP Function

function getZooElementData($item_id, $element_identifier, $element_child) {
  $db =& JFactory::getDBO();
  $query = "SELECT elements FROM #__zoo_item WHERE id = " . $item_id;
  $db->setQuery($query);
  $element_xml = $db->loadResult();
  $elements = simplexml_load_string($element_xml);
  foreach ($elements->children() as $element) {
    if((string)$element->attributes() == $element_identifier) {
      return (string)$element->$element_child;
    }
  }
}

PHP Usage

echo getZooElementData(2, '43aa1b15-986e-4303-afcf-628b835f10e5', 'value');
You would use the code above to extract a textarea from a zoo item with the id 2 from the following XML:
<textarea identifier="43aa1b15-986e-4303-afcf-628b835f10e5">  
  <value><![CDATA[<p>This is some sample yootheme zoo element data.</p>]]></value>
</textarea>

References

Hope this was helpful! 

blog comments powered by Disqus