1

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:

enter image description here

This is the desired row from my #__k2_extra_fields table:

This view from item

1

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.

| improve this answer | |
  • 1
    That's what made me confused when I tried to answer his question. – Thiago Guimarães Jul 6 at 22:29
  • Thank you @mickmackusa. You answer is great as usual. But maybe this time you missed smth. Code display nothing. I coldn't find the problem. – okisan Jul 7 at 5:29
  • Correct you are. The problem is not in the code -- value is an empty string. I have ported your data to a demo script for you to see for yourself. 3v4l.org/jXGMH What value were you expecting to see displayed? @oki – mickmackusa Jul 7 at 6:40
  • Ok. You are right that value is empty. But value that we are looking for is under item's table. How can we display it as "brand" value? Im sorry maybe i can't see your point. – okisan Jul 7 at 7:11
  • 1
    I had completely confused myself about the table structures. I have completely rewritten my answer. Let me know if there is something remaining to fix up. @oki – mickmackusa Jul 7 at 7:15
1

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;
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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