AnswerBun.com

How to display specific K2 extra field of current item with k2 tool module

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:

enter image description here

This is the desired row from my #__k2_extra_fields table:

This view from item

2 Answers

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

Add your own answers!

Related Questions

Generating a module instance from PHP

0  Asked on September 5, 2021 by ffrewin

   

Embed custom JS app in Joomla MVC

0  Asked on September 5, 2021 by miasto

     

Error when updating Gantry from 4.1.20 to 4.1.32

1  Asked on September 5, 2021 by norberto-gomes

       

Change user permissions programmatically on the fly

1  Asked on September 5, 2021 by aheavyobject

   

Akeeba backup update broke the site

2  Asked on September 5, 2021 by hamlet-kraskian

   

Proper way for autoloading in Joomla 4

1  Asked on September 5, 2021 by webchun

     

Menu Specifically For Super Users

1  Asked on September 5, 2021

       

Joomla PHP does not match host PHP

2  Asked on February 18, 2021

     

Controller Joomla – Output Class construct for color css style

1  Asked on February 5, 2021 by andrea-suriani

       

Joomla custom component – router not find alias

1  Asked on January 21, 2021 by lecraver

     

Joomla 3 call model function inside controller

3  Asked on December 28, 2020 by wbdevlop

   

Product url problem after updating VirtueMart

1  Asked on December 23, 2020 by kiasaty

     

Display intro image inside mod_breadcrumbs

0  Asked on November 11, 2020 by geoplous

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP