- Posts: 20
COMMUNITY FORUM
- Forum
- K2 Community Forum
- English K2 Community
- Get specific extra field from database to use outside of K2
Get specific extra field from database to use outside of K2
- Dario Pintarić
- Topic Author
- Offline
- Junior Member
Less
More
13 years 3 months ago #99273
by Dario Pintarić
Get specific extra field from database to use outside of K2 was created 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 ?
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.
- william white
- Offline
- Platinum Member
Less
More
- Posts: 3722
13 years 3 months ago #99274
by william white
Replied by william white on topic Get specific extra field from database to use outside of K2
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ć
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
13 years 3 months ago #99275
by Dario Pintarić
Replied by Dario Pintarić on topic Get specific extra field from database to use outside of K2
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 :(
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.
- william white
- Offline
- Platinum Member
Less
More
- Posts: 3722
13 years 3 months ago #99276
by william white
Replied by william white on topic Get specific extra field from database to use outside of K2
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
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ć
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
13 years 3 months ago #99277
by Dario Pintarić
Replied by Dario Pintarić on topic Get specific extra field from database to use outside of K2
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....
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.
- Cafe Bubble
- Offline
- New Member
Less
More
- Posts: 1
10 years 3 months ago #99278
by Cafe Bubble
Replied by Cafe Bubble on topic Re: Get specific extra field from database to use outside of K2
I need it too. Can you help please...
Please Log in or Create an account to join the conversation.
- EMSA
- Offline
- New Member
Less
More
- Posts: 6
4 years 4 months ago #176433
by EMSA
Replied by EMSA on topic Get specific extra field from database to use outside of K2
Would be really nice if u can share here that query.
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
4 years 4 months ago #176439
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic Get specific extra field from database to use outside of K2
See this please: getk2.org/documentation/tips-a-tricks/display-single-extra-fields-anywhere-in-your-k2-content
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- EMSA
- Offline
- New Member
Less
More
- Posts: 6
4 years 4 months ago - 4 years 4 months ago #176449
by EMSA
Replied by EMSA on topic Get specific extra field from database to use outside of K2
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; ?>
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.
- Okan
- Offline
- New Member
Less
More
- Posts: 8
4 years 4 months ago #176461
by Okan
Replied by Okan on topic Get specific extra field from database to use outside of K2
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
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.
- Forum
- K2 Community Forum
- English K2 Community
- Get specific extra field from database to use outside of K2