- Posts: 10
COMMUNITY FORUM
Cant render extrafield in category item view
- Artonezero Limited
-
Topic Author
- Offline
- New Member
I've tried many solutions to display 2 of 4 extra fields category_item.php but every time i get the following :
Trying to get property of non-object
Extra field is called Length and Alias is Length
I add the following into category_item.php inside my template folder ( its int he correct place as the rest of the page is rendering correctly):
$this->item->extra_fields->Length->value;
also tried:
//convertArray to use ids as key
$extrafields = array();
foreach($this->item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
?>
// Render a specific extrafield
<td>
<?php echo $extrafields[1]; ?>
</td>
<td>
<?php echo $extrafields[4]; ?>
</td>
This time the error is
Invalid argument supplied for foreach().... Undefined offset:1.... Undefined offset:4....
Has it changed since 2.7.1 or something else i need to add / change?
I've loooked at github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L104-L109
and tried various other methods seen in forum answers, but always get this error message and no output. there is a value inside the
Any help appreciated.
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
$this->item->extra_fields->Length->value;
Make sure that it is length and not Length.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Artonezero Limited
-
Topic Author
- Offline
- New Member
- Posts: 10
Thansk for replying quickly. Nothing is output in the table. Here's the category_item.php in full, maybe i've removed something crucial?
// no direct access
<?php defined('_JEXEC') or die;
// Define default image size (do not change)
K2HelperUtilities::setDefaultImage($this->item, 'itemlist', $this->params);
?>
<!-- Start K2 Item Layout -->
<?php if($this->item->params->get('catItemTitle')): ?>
<!-- Item title -->
<td class="catItemTitle">
<?php if ($this->item->params->get('catItemTitleLinked')): ?>
<a href="<?php echo $this->item->link; ?>">
<?php echo $this->item->title; ?>
</a>
<?php else: ?>
<?php echo $this->item->title; ?>
<?php endif; ?>
<?php if($this->item->params->get('catItemFeaturedNotice') && $this->item->featured): ?>
<!-- Featured flag -->
<span>
<sup>
<?php echo JText::_('K2_FEATURED'); ?>
</sup>
</span>
<?php endif; ?>
</td>
<?php endif; ?>
<td>
<?php echo $this->item->extra_fields->Length->value; ?>
</td>
<td>
<?php echo $this->item->extra_fields->Weight->value; ?>
</td>
<?php if($this->item->params->get('catItemIntroText')): ?>
<!-- Summary -->
<td>
<?php echo $this->item->introtext; ?>
</td>
<?php endif; ?>
<!-- End K2 Item Layout -->
Please Log in or Create an account to join the conversation.
- Artonezero Limited
-
Topic Author
- Offline
- New Member
- Posts: 10
Trying to get property of non-object
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
<?php var_dump($this->item->extra_fields); ?>
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Artonezero Limited
-
Topic Author
- Offline
- New Member
- Posts: 10
string(411) "[{"id":"1","value":"10' 3\" - 3.11m"},{"id":"2","value":"4' 0\" - 1.22m"},{"id":"3","value":"-"},{"id":"4","value":"45"},{"id":"5","value":"54 sq ft - 5.2 sq m"},{"id":"6","value":"150-220 hours"},{"id":"7","value":["Suggested Hardware Lists","#","same"]},{"id":"8","value":["Buy the plan","#","same"]},{"id":"9","value":["Download a study plan","#","same"]},{"id":"10","value":["Buy a study plan","#","same"]}]"
Please Log in or Create an account to join the conversation.
- Artonezero Limited
-
Topic Author
- Offline
- New Member
- Posts: 10
array(10) { [0]=> object(stdClass)#605 (8) { ["id"]=> string(1) "1" ["name"]=> string(6) "Length" ["value"]=> string(14) "10' 3" - 3.11m" ["type"]=> string(9) "textfield" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "1" ["alias"]=> string(6) "length" } [1]=> object(stdClass)#604 (8) { ["id"]=> string(1) "2" ["name"]=> string(4) "Beam" ["value"]=> string(13) "4' 0" - 1.22m" ["type"]=> string(9) "textfield" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "2" ["alias"]=> string(4) "Beam" } [2]=> object(stdClass)#651 (8) { ["id"]=> string(1) "3" ["name"]=> string(5) "Draft" ["value"]=> string(1) "-" ["type"]=> string(9) "textfield" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "3" ["alias"]=> string(5) "Draft" } [3]=> object(stdClass)#652 (8) { ["id"]=> string(1) "4" ["name"]=> string(11) "Weight (kg)" ["value"]=> string(2) "45" ["type"]=> string(9) "textfield" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "4" ["alias"]=> string(8) "weightkg" } [4]=> object(stdClass)#653 (8) { ["id"]=> string(1) "5" ["name"]=> string(9) "Sail Area" ["value"]=> string(19) "54 sq ft - 5.2 sq m" ["type"]=> string(9) "textfield" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "5" ["alias"]=> string(8) "SailArea" } [5]=> object(stdClass)#654 (8) { ["id"]=> string(1) "6" ["name"]=> string(24) "Time to build (estimate)" ["value"]=> string(13) "150-220 hours" ["type"]=> string(9) "textfield" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "6" ["alias"]=> string(19) "Timetobuildestimate" } [6]=> object(stdClass)#655 (11) { ["id"]=> string(1) "7" ["name"]=> string(24) "Suggested Hardware Lists" ["value"]=> string(41) "Suggested Hardware Lists" ["type"]=> string(4) "link" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "7" ["url"]=> string(1) "#" ["text"]=> string(24) "Suggested Hardware Lists" ["attributes"]=> string(0) "" ["alias"]=> string(21) "SuggstedHardwareLists" } [7]=> object(stdClass)#656 (11) { ["id"]=> string(1) "8" ["name"]=> string(12) "Buy the plan" ["value"]=> string(29) "Buy the plan" ["type"]=> string(4) "link" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "8" ["url"]=> string(1) "#" ["text"]=> string(12) "Buy the plan" ["attributes"]=> string(0) "" ["alias"]=> string(10) "Buytheplan" } [8]=> object(stdClass)#657 (11) { ["id"]=> string(1) "9" ["name"]=> string(21) "Download a study plan" ["value"]=> string(38) "Download a study plan" ["type"]=> string(4) "link" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "9" ["url"]=> string(1) "#" ["text"]=> string(21) "Download a study plan" ["attributes"]=> string(0) "" ["alias"]=> string(18) "Downloadastudyplan" } [9]=> object(stdClass)#658 (11) { ["id"]=> string(2) "10" ["name"]=> string(16) "Buy a study plan" ["value"]=> string(33) "Buy a study plan" ["type"]=> string(4) "link" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(2) "10" ["url"]=> string(1) "#" ["text"]=> string(16) "Buy a study plan" ["attributes"]=> string(0) "" ["alias"]=> string(13) "Buyastudyplan" } }
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
The alias is not present.
The alias is present in the category as well.
I assume you are using K2's latest version. Are you using any other K2 related extensions?
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Artonezero Limited
-
Topic Author
- Offline
- New Member
- Posts: 10
No other extensions on the entire site. Just Joomla a basic custom template and K2 presently.
It's a staging / development site.
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Artonezero Limited
-
Topic Author
- Offline
- New Member
- Posts: 10
same errors. Dump below:
string(411) "[{"id":"1","value":"10' 3\" - 3.11m"},{"id":"2","value":"4' 0\" - 1.22m"},{"id":"3","value":"-"},{"id":"4","value":"45"},{"id":"5","value":"54 sq ft - 5.2 sq m"},{"id":"6","value":"150-220 hours"},{"id":"7","value":["Suggested Hardware Lists","#","same"]},{"id":"8","value":["Buy the plan","#","same"]},{"id":"9","value":["Download a study plan","#","same"]},{"id":"10","value":["Buy a study plan","#","same"]}]"
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
You can use json_encode() php.net/manual/en/function.json-decode.php in order to convert it into an array so you can directly print data based on the id's position.
Are you using the default template or an override?
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Artonezero Limited
-
Topic Author
- Offline
- New Member
- Posts: 10
Here's the code for reference:
<td>
<?php
//json_decode the extra_fields string
$json = json_decode($this->item->extra_fields);
// print Length if exists
if($json[0]!=''): print $json[0]->value;
endif;?>
</td>
<td>
<?php
// print Weight if exists
if($json[3]!=''): print $json[3]->value;
endif; ?>
</td>
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.