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!