2

I'm having trouble getting my values from my array to print out in a twig file.

Right now I have this:

ProductController.php on line 290:
array:1 [▼
  1 => array:3 [▼
    0 => "Water"
    1 => 5
    2 => 2.75
  ]
]

In twig I have this:

        <tbody>
            {% for cartValue in cartArray %}
                <tr>
                    <!--ERRORS BELOW NOW-->
                    <td>{{ cartValue }}</td>
                    <td>{{ quantity }}</td>
                    <td>${{ price }}</td>
                </tr>
            {% endfor %}    
        </tbody>
    </table> <!--top table-->

In productEntity I have this:

    $cartArray = array();

    if (is_null($cartArray) || !$entity) {
        throw $this->createNotFoundException('Error: Nothin in Array/Entity');
    } else {
        $cartArray = $session->get('cartArray', []);
        // $cartArray[$entity->getId()] = $entity->getName();
        $cartArray[$entity->getId()] = [$entity->getName(), $entity->getQuantity(), $entity->getPrice()];

        foreach ($cartArray as $key => $product) {
                //dump($cartArray); die;
                //dump($key); die;
                $productEntity = $em->getRepository('PaTShopTestBundle:Product')->find($key);
                $quantity = $productEntity->getQuantity();
                $price = $productEntity->getPrice();
                $totalCostOfAllProducts += $price * $quantity;
        }
    }

    $session->set('cartArray', $cartArray); //session---------------

    //var_dump($cartArray); die;

    return array(
        'price'     => $price,
        'quantity'  => $quantity,
        'totalCostOfAllProducts'   => $totalCostOfAllProducts,
        'cartArray'   => $cartArray,
        'entity'      => $entity,
        'delete_form' => $deleteForm->createView(),
    );

I want to be able to show the cartValue(name of product) quantity and price of each product in my array. If I add a second item to array I get this:

array:2 [▼
  1 => array:3 [▼
    0 => "Water"
    1 => 5
    2 => 2.75
  ]
  2 => array:3 [▼
    0 => "Duck"
    1 => 3
    2 => 6.25
  ]
]

I keep getting some errors, this one being :

An exception has been thrown during the rendering of a template ("Notice: Array to string conversion") in src/PaT/ShopTestBundle/Resources/views/Product/cart.html.twig at line 21.

I've tried referencing the index's of the array

In twig:

{{ cartValue[0] }}
{{ quantity[1] }}

but it stops working once quantity[1] is called: Error:

Impossible to access a key ("1") on a integer variable ("3") in src/PaT/ShopTestBundle/Resources/views/Product/cart.html.twig at line 22

I know there is a way to do what I'm trying to accomplish but I just don't know it. I'm a newbie but I'm learning.

Any Help is truly appreciated! Thanks!

2 Answers 2

1

Your array loop in Twig is not proper..

If you do not want to use keys do this..

EDIT: INDEXED ARRAY

<tbody>
    {% for key, cartValue in cartArray %}
        <tr>
            <!--ERRORS BELOW NOW-->
            <td>{{cartValue[0]}}</td>
            <td>{{cartValue[1]}}</td>
            <td>{{cartValue[2]}}</td>
        </tr>
    {% endfor %}    
</tbody>

With keys.. Modify your cartArray in PHP to look like this

      $cartArray[$entity->getId()] = [
        'name' => $entity->getName(),
        'quantity' => $entity->getQuantity(),
        'price' => $entity->getPrice()
      ];

Then in your Twig file do this,

        <tbody>
        {% for cartValue in cartArray %}
            <tr>
                <!--ERRORS BELOW NOW-->
                <td>{{ cartValue.name }}</td>
                <td>{{ cartValue.quantity }}</td>
                <td>${{ cartValue.price }}</td>
            </tr>
        {% endfor %}    
    </tbody>
</table> <!--top table-->
Sign up to request clarification or add additional context in comments.

5 Comments

I see that now, thank you. I do however get Error: Key "name" for array with keys "0, 1, 2" does not exist in src/PaT/ShopTestBundle/Resources/views/Product/cart.html.twig at line 21. Any quick ideas as to why?
did you add keys(name, quantity, price) to your $cartArray ?
I did $cartArray[$entity->getId()] = [ 'name' => $entity->getName(), 'quantity' => $entity->getQuantity(), 'price' => $entity->getPrice() ];
I have made some edits, try passing key explicitly in the twig loop and revert your changes in $cartArray(as it was earlier - indexed)
I got it with your edit. Just added key to my twig for loop. Thanks again!
0

    {% for cartValue in cartArray %}
        <tr>
            <!--ERRORS BELOW NOW-->
            <td>{{ cartArray[0] }}</td>
            <td>{{ cartArray[1] }}</td>
            <td>${{ cartArray[2] }}</td>
        </tr>
    {% endfor %}    
</tbody>

or

$cartArray[$entity->getId()] = array('name' =>$entity->getName(), 'quantity' => $entity->getQuantity(), 'price' => $entity->getPrice());

Comments

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.