1

I have a function in which I count the final price of each product and I save it in an array. Now when I want do display the values I am stuck at how to display all of them dynamicaly.

This is the function:

public function getTotal($items)
{
    $total = array();
    foreach($items as $key=>$item){
        $total[$key] = $item->getPrice() - $item->getDiscount() + $item->getValue();
    }
    return $total;
}

All of the methods getValue, getDiscount and etc are working.

This is how I try o display:

{{ getTotal(product)[key]}}

The problem is that when I write for example {{ getTotal(product)[0]}} or {{ getTotal(product)[1]}} and etc i get the correct value, but only of 1 product. I need to get the values from all of them.

If i do {{ getTotal(product)[key]}} im getting a strange error:

Key "12" for array with keys "0, 1" does not exist in MpShopBundle:Frontend:product_summary.html.twig at line 89

I have no idea why the key is equal to 12? Maybe I have to write something different?

UPDATE Thank you for the answers, I didnt even think about looping with twig, but I am finally getting some values. However, I dont know why but the twig loop assigns both values for each product.

This is how it should be:

Product1 : 0(key):145(value)
Product2 : 1:415

But this is how it is:

Product1 : 0:145 1:415
Product2 : 0:145 1:415

This is the twig:

 {% if product is defined %}

            {% for key, item in cart %}

              {% for item in product %}


                <tr>

                  <td> <img width="60" src="{{ asset('bundles/mpFrontend/assets/products/4.jpg') }}" alt=""/></td>

                  <td>{{ item.model }}</td>
                  <td>
                    <div class="input-append"><input class="span1" style="max-width:34px" placeholder="1" id="appendedInputButtons" size="16" type="text">
                    <button class="btn" type="button"><i class="icon-minus"></i></button>
                    <button class="btn" type="button"><i class="icon-plus"></i></button>
                    <button class="btn btn-danger" type="button"><a href="{{ path('cart_remove', {'id': key}) }}"><i class="icon-remove icon-white"></i></button>
                    </div>
                  </td>

                  <td>{{ item.price }}</td>
                  <td>{{ item.discount }}</td>
                  <td>{{ item.value }}</td>

                  {% if getTotal(product) is iterable %}
                    {% for key, sum in getTotal(product) %}

                     <td>{{ key }}:{{ sum }}</td>

                  {% endfor %}
                {% endif %}

                </tr>

 {% endfor %}
3
  • The error is saying that you have only keys "1" and "2" for your array. Commented Apr 27, 2015 at 10:49
  • Yes for now I have only 2 products, so the first product is key 0 and the second is 1 Commented Apr 27, 2015 at 10:50
  • So you are setting key as 12 somewhere else in your code.. I can't see that, you need to show us the rest of your code. Commented Apr 27, 2015 at 10:53

3 Answers 3

0

Use twig for loop:

{% for total in getTotal(product) %}
  {{ total }}
{% endfor %}
Sign up to request clarification or add additional context in comments.

2 Comments

You are messing up the same variable names in the for cycles. The variable names (key, item) must be unique. So it's also hard for us to guess, what you want to display. But for now it seems, you should change the cycles to something like: {% for cartKey, cartItem in cart %} and {% for productKey, productItem in product %}.
Then you could remove the cycle for getTotal(product) and jut use {{ getTotal(product)[productKey] }}.
0

You can use the twig for loop.

Example:

{% if getTotal(product) is iterable %}
    {% for key, sum in getTotal(product) %}
        {{ key }}:{{ sum }}
    {% else %}
        empty array
    {% endfor %}
{% endif %}

1 Comment

You should not use the same var names in both of your loops (key)
0

You shouldn't use a loop, and move your function from an external to a method of the product class.

Instead of loop:

<td>{{ product.total }}</td>

New function:

class Product {
...
    public function getTotal()
    {
        return $this->getPrice() - $this->getDiscount() + $this->getValue();
    }
}

2 Comments

That messes up all of my functions. Why is my code bad?
Changed my answer, new solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.