Joomla Asked by okisan on September 5, 2021
I am trying to display specific extra field values of a specific item with the K2 tool module.
I have tried to get just BRAND
, but I’m getting stdClass::brand() error.
$db = JFactory::getDBO();
$articleid = JRequest::getInt('id');
$db->setQuery('SELECT extra_fields FROM #__k2_items WHERE id='.$articleid);
$AllExtraFields = $db->loadResult();
$AllExtraFields = json_decode($AllExtraFields);
$lists[$i]->brand= $AllExtraFields[1]->value;
$myid=$lists[$i]->brand();
echo $myid;
This is a row from my #__k2_items
table:
This is the desired row from my #__k2_extra_fields
table:
I made it like this and it works.
<?php
$db = JFactory::getDBO();
$articleid = JRequest::getInt('id');
$db->setQuery('SELECT extra_fields FROM #__k2_items WHERE id='.$articleid);
$AllExtraFields = $db->loadResult();
$AllExtraFields = json_decode($AllExtraFields);
echo $AllExtraFields[0]->value;
?>
Correct answer by okisan on September 5, 2021
Start by extracting the extra_fields
column's json string from #__k2_items
table at the row of your choosing, then decode it. Each row from the array (generated by decoding json), will have two elements having respective keys: id
and value
.
I don't quite know the full picture of what you are trying to do, but I can help you to relate and extract the data.
$output = [];
$db = JFactory::getDBO();
$db->setQuery('SELECT extra_fields FROM #__k2_items WHERE id = ' . JRequest::getInt('id'));
foreach (json_decode($db->loadResult() ?: '[]', true) as ['id' => $id, 'value' => $value]) {
// Access the extra_fields using this row's id
$db->setQuery('SELECT * FROM #__k2_extra_fields WHERE id = ' . (int)$id);
$row = $db->loadAssoc();
$output[] = [
'id' => $id,
'value' => $value,
'extraFieldName' => $row['name'],
'extraFieldType' => $row['type'],
'decoded' => json_decode($row['value'], true)
];
}
var_export($output);
This will effectively display the following:
[
[
'id' => '1',
'value' => 'PERKINS',
'extraFieldName' => 'BRAND',
'extraFieldType' => 'textfield',
'decoded' => [
[
'name' => null,
'value' => '',
'target' => null,
'alias' => 'brand',
'required' => 1,
'showNull' => 0
]
]
],
[
'id' => '2',
'value' => '14',
// more data that I can't see...
],
]
I have seen snippets in K2 such as $item->extra_fields = $model->getItemExtraFields($item->extra_fields, $item);
which indicate that there are some pre-existing helpful methods on offer, but I must admit that I am not a K2 user, so I am poorly suited to advise on these.
Answered by mickmackusa on September 5, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP