Keyword

Retrieve a single extra_field value

  • Massimo Zanotto
  • Massimo Zanotto's Avatar Topic Author
  • Offline
  • New Member
More
8 years 5 months ago #155859 by Massimo Zanotto
Retrieve a single extra_field value was created by Massimo Zanotto
Hi all,
i'm creating a form with RSFormPro and I need a dropdown list where I would like to show a specific value from K2 extra_fields.

I did it retrieving all the extra_fields using $json->decode and displaying only one, but this is very time consuming slowing down the page load.

Is there a K2 function to retrieve directly a specific extra_field value giving only the id of the item and the id of the extra_field?

Thanks in advance,
Max

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 5 months ago #155862 by Krikor Boghossian
Replied by Krikor Boghossian on topic Retrieve a single extra_field value
Ok, I have the code halfway ready,
Look at this post -> www.joomlaworks.net/forum/k2-en/46055-programmatically-grabbing-a-list-of-item-titles#155820

With this code you can load items which match certain criteria.
Once you get the object you can access all of its properties.

In order to display only a specific extrafield based on its alias you need this code -> github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L105-L109

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

  • Massimo Zanotto
  • Massimo Zanotto's Avatar Topic Author
  • Offline
  • New Member
More
8 years 4 months ago - 8 years 4 months ago #155973 by Massimo Zanotto
Replied by Massimo Zanotto on topic Retrieve a single extra_field value
For everyone else who will need this in the future, this is what i did:

Insert the following code directly inside the dropdown field in RSFormPro
//<code>
JLoader::register('Services_JSON', JPATH_ADMINISTRATOR.DS.'components'.DS.'com_k2'.DS.'lib'.DS.'JSON.php');
$jinput = JFactory::getApplication()->input;

$nl = "\n";
$output = "|-- Select a product --".$nl;

$db = JFactory::getDBO();
$query = $db->getQuery(true);

$query = "SELECT p.id,p.title, p.extra_fields, p.alias, c.name AS category FROM `#__k2_items` AS p INNER JOIN (`#__k2_categories` AS c) ON (p.catid = c.id) WHERE p.published = 1 AND p.trash = 0  AND c.parent = 1 ORDER BY c.name ASC, TRIM(p.title) ASC";

$db->setQuery($query);
$products_list = $db->loadObjectList();

$p_cat = "";
$tot_prod = count($products_list);
$x = 1;

$json = new Services_JSON;
foreach($products_list AS $p){

	$jsonObjects = $json->decode($p->extra_fields);

	
        // $code is the the name of the only extra_fields that i need, but even the other extra_fields are inside the $jsonObject
        // so if you need another extra_field you can write: list($xf1,$xf2,$xf3,etc...,etc...,etc...) = $jsonObjects;
	list($code) = $jsonObjects; 
	
	if($p->category != $p_cat) {
		$output .= $p->category."[g]".$nl;
	}
	
$current_jinput_id = $p->id.$p->alias;
$checked = $jinput->get('id') == $current_jinput_id ? "[c]" : "";

	$output .= $p->id."|".$code->value." - ".$p->title."".$checked;

	if($x != $tot_prod) {
		$output .= $nl;
	}
	
	$p_cat = $p->category;
	$x++;
}
return $output;
//</code>

Hope this help
Max
Last edit: 8 years 4 months ago by Massimo Zanotto.

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 4 months ago #155976 by Krikor Boghossian
Replied by Krikor Boghossian on topic Retrieve a single extra_field value
Awesome :)

Kudos Max!

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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


Powered by Kunena Forum