Display repeatable element by name in custom template

otinanaipali

New Member
I use a custom template to display elements by name.

This is what I use to display the field "supplier_name" from group "supplier":
Code:
$this->element = $this->groups['supplier']->elements['supplier_name'];
echo $this->loadTemplate('element');
This displays elements from groups that are not repeatable.

However, when it comes to repeatable groups this code doesn't behave as I expected. What php code should I use to display a field from a repeatable group? Let's say I have a repeatable group "products", and I need to display all repeated values of the element "product_description"

This is what I tried but it doesn't work:
Code:
$subgroup = $this->groups['products']->subgroups;
$this->element = $this->subgroup->elements['product_description'];
echo $this->loadTemplate('element');

If I use this:
Code:
$this->element = $this->groups['products']->elements['product_description'];
echo $this->loadTemplate('element');
It just displays only the last field value of the repeatable group.

Please for some help here.
 
Code:
$subgroup = $this->groups['products']->subgroups;

As its a repeat group, $subgroups is an array of elements so you need to iterate over it as follows:

PHP:
foreach ($this->groups['products']->subgroups as $subgroup) :
  $this->element = $subgroup->elements['product_description'];
  echo $this->loadTemplate('element');
endforeach;
 
I tried this but it doesn't seem to work. I took "contacts_custom" template, and I created a new template. The edits I made are as below:

In file "default_element.php" I have this code:
Code:
<?php if ($group->canRepeat) {
foreach ($group->subgroups as $subgroup) { ?>
        <?php $this->elements = $subgroup; echo $this->loadTemplate('group'); ?>
<?php }
} else { ?>
<span <?php echo @$this->element->column;?> class="<?php echo $this->element->containerClass;?>">
        <?php echo $this->element->element;?>
</span>
<?php }
$this->element->rendered = true; ?>

In file "default_group.php" I use:
Code:
<table>
  <tr><td>
    <?php $this->element = $this->groups['supplier']->elements['supplier_bill_to'];echo $this->loadTemplate('element'); ?>
  </td></tr>
  <tr><td>
    <?php foreach ($this->groups['product']->subgroups as $subgroup) {  $this->element = $subgroup->elements['product_description'];  echo $this->loadTemplate('element'); } ?>
  </td></tr>
</table>
What am I doing wrong here and it doesn't display "product_description" at all from the group "product"? Not even one of the repeated group...
 
are you sure that is what you have in default_element.php? It doesn't look like the contents of my default_elements.php file. You would not want to be re-loading the group template from the element template.
To start with simply work in default_group.php.
Using:

PHP:
$subGroups = $this->groups['products']->subgroups;
foreach ($subGroups as $subGroup)
{
  $this->element = $subGroup['product_description'];
  echo $this->loadTemplate('element');
}
I have tested and this works fine for me.

hope that helps!
-Rob
 
Hi Rob,

Yes! this piece of code now works like a charm! In the previous reply you had a slightly different code and it wasn't working. I'm not sure why, but I'm not php developer. I just do frontend development/design. Now it works as expected.

Thank you for your help.
 
Hi Rob,

One last question:
What's wrong with this and it's not displaying the sum of the array?
Code:
 $subGroups = $this->groups['product']->subgroups;
$totalMe = 0;
foreach ($subGroups as $subGroup){
    $this->element = $subGroup['product_price_total'];
    $thisEach = floatval($this->loadTemplate('element'));
    $totalMe += $thisEach;
}
echo $totalMe;

I always get "0". If I change the code to "$totalMe += $this->element;" I get the sum of array entries and I need the sum of values of the array.
 
OK! I got it... "strip_tags()" is my answer... each array value holds html tags and I had to strip these tags and keep only the values.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top