Keyword

[SOLVED] Load extra fields from a other item

  • Gerben
  • Gerben's Avatar Topic Author
  • Offline
  • Senior Member
More
9 years 10 months ago - 9 years 10 months ago #133876 by Gerben
Load extra fields from a other item was created by Gerben
Hi Support,

I'm working on a project where i need to load extra fields from other specific item (with id 18) on a categorie item :)

So i have the following code that is getting all the extra fields from item 18.

<?php $db = & JFactory::getDBO();
$query = $db->getQuery(true)
->select('extra_fields')
->from('#__k2_items')
->where('id= 18');
$db->setQuery($query);
$extrafields = $db->loadObjectlist();
?>

How can i encode this string so i can echo every extra field on its selve?
Really breaking my head about it :)
Hope that someone can push me in the right direction.
Last edit: 9 years 10 months ago by Gerben.

Please Log in or Create an account to join the conversation.

  • Mohamed Abdelaziz
  • Mohamed Abdelaziz's Avatar
  • Offline
  • Platinum Member
  • Joomla Developer
More
9 years 10 months ago #133902 by Mohamed Abdelaziz
Replied by Mohamed Abdelaziz on topic Load extra fields from a other item
Hi Gerben,

A better approach is to use the K2Item model object.

Multiple Extra Fields Groups for K2
AutoMeta for K2
Chained Fields for K2
More K2 Extensions In My Extensions Store

Please Log in or Create an account to join the conversation.

  • Gerben
  • Gerben's Avatar Topic Author
  • Offline
  • Senior Member
More
9 years 10 months ago #133906 by Gerben
Replied by Gerben on topic Load extra fields from a other item
Hi Mohamed.

Thank you for your answer but i want to do it manualy.
the var_dump gives me this:

array(1) { [0]=> object(stdClass)#284 (1) { ["extra_fields"]=> string(1556) "[{"id":"1","value":"009d3e"},{"id":"2","value":"005c25"},{"id":"3","value":"ffed00"},]" } }

It must be possible to just echo the value of id 2 but i can not manage to fix this until so far.
Somebody had any idea?

Cheers

Please Log in or Create an account to join the conversation.

  • Mohamed Abdelaziz
  • Mohamed Abdelaziz's Avatar
  • Offline
  • Platinum Member
  • Joomla Developer
More
9 years 10 months ago - 9 years 10 months ago #133928 by Mohamed Abdelaziz
Replied by Mohamed Abdelaziz on topic Load extra fields from a other item
No problem, it can be done by this code
	require_once (JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_k2' . DS . 'lib' . DS . 'JSON.php');
        $json = new Services_JSON;
        $jsonObjects = $json->decode($extrafields[0]);

BTW, the query you used will return only one value, so it is better to use $db->loadResult() instead of $db->loadObjectList()

Multiple Extra Fields Groups for K2
AutoMeta for K2
Chained Fields for K2
More K2 Extensions In My Extensions Store
Last edit: 9 years 10 months ago by Mohamed Abdelaziz.

Please Log in or Create an account to join the conversation.

  • Gerben
  • Gerben's Avatar Topic Author
  • Offline
  • Senior Member
More
9 years 10 months ago #133934 by Gerben
Replied by Gerben on topic Load extra fields from a other item
Thank you Mohamed for your help.

I managed it by doing:

require_once (JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_k2' . DS . 'lib' . DS . 'JSON.php');
?>

<?php $db = & JFactory::getDBO();
$query = $db->getQuery(true)
->select('extra_fields')
->from('#__k2_items')
->where('id= 18');
$db->setQuery($query);
$extrafields = $db->loadResult();
require_once (JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_k2' . DS . 'lib' . DS . 'JSON.php');
$json = new Services_JSON;
$jsonObjects = $json->decode($extrafields);

echo $jsonObjects[3]->value;

?>


<? print_r($jsonObjects);?>

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum