Keyword

Get specific extra field from database to use outside of K2

  • Dario Pintarić
  • Dario Pintarić's Avatar Topic Author
  • Offline
  • Junior Member
More
13 years 3 months ago #99273 by Dario Pintarić
Hi,

I'm working on some customization on a RokStories module and i got caught up with few problems...

As I see it, RokStories is querying items from DB, and now i have a big problem....

1. each item has two extrafields, and i would like to display the second extrafield - how do i do that ?

 

So far i've been able to pull out the items date of creation and categoryname by editing RokStories "helper.php"....

 

Here's the K2 references inside RokStories helper.php

 

require_once (JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'route.php');                        $query = "SELECT a.*, c.name as categoryname,c.id as categoryid, c.alias as categoryalias, c.params as categoryparams".            " FROM #__k2_items as a".            " LEFT JOIN #__k2_categories c ON c.id = a.catid";                $query .= " WHERE a.published = 1"            ." AND a.access <= {$aid}"            ." AND a.trash = 0"            ." AND c.published = 1"            ." AND c.access <= {$aid}"            ." AND c.trash = 0"            ;                   if ($params->get('catfilter')){                if (!is_null($cid)) {                    if (is_array($cid)) {                        $query .= " AND a.catid IN(".implode(',', $cid).")";                    }                    else {                        $query .= " AND a.catid={$cid}";                    }                }            }            if ($params->get('FeaturedItems')=='0')                $query.= " AND a.featured != 1";                if ($params->get('FeaturedItems')=='2')                $query.= " AND a.featured = 1";                            $query .= $where . ' ORDER BY ' . $orderby;            // end K2 specific        }                                $db->setQuery($query, 0, $count);        $rows = $db->loadObjectList();        $i=0;        $lists    = array();                if (is_array($rows) && count($rows)>0) {            foreach ( $rows as $row )            {                //process content plugins               $text = JHTML::_('content.prepare',$row->introtext,$cparams);                $lists[$i]->id = $row->id;                $lists[$i]->created = $row->created;                $lists[$i]->modified = $row->modified;                $lists[$i]->title = htmlspecialchars( $row->title );                $lists[$i]->introtext = modRokStoriesHelper::prepareContent($text, $params);                $lists[$i]->garantirano = $row->garantirano;                $lists[$i]->kategorija = $row->categoryname;

 

p.s. this goes and although i'm not a php pro i think i'll have to query/get the extra fields from DB and use helper or something to get the extra field...

 

Any one have any idea ?

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

More
13 years 3 months ago #99274 by william white
Since the xtra fields are stored in a.extra_fields can you "explode" them into an array just after the select statement and then call the array element (1) for the second extra field?

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

  • Dario Pintarić
  • Dario Pintarić's Avatar Topic Author
  • Offline
  • Junior Member
More
13 years 3 months ago #99275 by Dario Pintarić
OK, one thing i noticed that u said a.extra_fields so i added it to query and i got the entire content of my extra_field related to every item :)

now the problem is.... i never worked with the php's explode function and frankly i have no idea what to do now.i can't believe no1 did anything like this... or I just can't fin it :(

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

More
13 years 3 months ago #99276 by william white
If you got all the xtra fields for all the items, then once you get the reference to the item you need to do another query to get just that item and its extra field's field and explode it.

There are ways to get at it more easily from within k2 overrides, but if you are working in another component or extension you will have to pick the data you want out

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

  • Dario Pintarić
  • Dario Pintarić's Avatar Topic Author
  • Offline
  • Junior Member
More
13 years 3 months ago #99277 by Dario Pintarić
Looks like explode was a very bad idea..... actually the content of the DB row is JSON encoded....

so i simply did:

 

// Prepare extra fields                    $AllExtraFields = $row->extra_fields; //get the content from the DB row                    $AllExtraFields = json_decode($AllExtraFields); //JSON decode into an array                $lists[$i]->grad = $AllExtraFields[1]->value; //think there's no need to explain the rest                $lists[$i]->cijena = $AllExtraFields[2]->value;

 

 

P.S. if any1 else need's to get extra fields from outside of K2, feel free to PM me....

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

More
10 years 3 months ago #99278 by Cafe Bubble
I need it too. Can you help please...

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

More
4 years 4 months ago #176433 by EMSA
Would be really nice if u can share here that query.

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

More
4 years 4 months ago #176439 by JoomlaWorks

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

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

More
4 years 4 months ago - 4 years 4 months ago #176449 by EMSA
Thank you Fotis.
But Dario was trying to display those values somewhere on the page with query. Not in k2 files.

Also, I have tried to use those codes that you mentioned like this but unfortunately didn't work in item.php (override)

<?php echo $this->item->extraFields->marka->name; ?>
Last edit: 4 years 4 months ago by EMSA. Reason: extra info

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

More
4 years 4 months ago #176461 by Okan
I made it.
Here is final code.

<?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;
?>

Regards

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


Powered by Kunena Forum