在 Symfony 中使用 foreach 循环和会话显示结果 PHP
Display results using foreach loop and sessions in Symfony for PHP
我正在使用 Symfony 中的会话为 PHP 编写鞋店网站的购物车页面。它可以将选定尺寸的商品添加到购物车并计算数量。我需要知道如何使用 foreach 循环显示购物车。
添加商品功能如下:
public function add($shoesid, $sizeid, SessionInterface $session)
{
$cart = $session->get('cart', []);
if(!empty($cart[$shoesid][$sizeid])) {
$cart[$shoesid][$sizeid]++;
} else {
$cart[$shoesid][$sizeid] = 1;
}
$session->set('cart', $cart);
dd($session->get('cart'));
}
这是我添加项目时收到的消息:
> array:2 [▼
40024 => array:1 [▼
410 => 2
]
]
(其中 40024 是 shoesid,410 是 sizeid,2 是数量([shoesid][sizeid] 的数量)
这里有一个显示购物车的函数
public function index(SessionInterface $session, ShoesRepository $shoesRepository, CountriesRepository $countriesRepository, StockRepository $stockRepository)
{
$cart = $session->get('cart', []);
$cartWithData = [];
foreach($cart as $shoesid => [$sizeid => $quantity]) //error here
{
$cartWithData[] = [
'shoe' => $shoesRepository->shoeDescr($shoesid),
'shoePrice' => $shoesRepository->find($shoesid),
'quantity' => $quantity,
'size' => $stockRepository->shoeSizeCart($shoesid, $sizeid)
];
}
$total=0;
$delivery=20;
foreach($cartWithData as $item)
{
$totalItem = $item['shoePrice']->getShoesPrice()*$item['quantity'];
$total += $totalItem;
}
$countries = $countriesRepository->findBy(array(), array('countryname' => 'asc'));
dd($cartWithData);
}
但是当我尝试启动购物车页面时出现下一个错误(第 9 行):
Notice: Undefined variable: sizeid
我应该更改哪些代码才能正确显示项目
$cart
似乎是一个多维数组(确切地说是两个),具有以下结构:
$cart = [
<$shoeId> => [
<$sizeId> => <count>,
<$sizeId> => <count>,
...,
],
<$shoeId> => [
...
],
];
您应该为每个级别设置一个 foreach()
循环来迭代。在这种情况下,您应该有两个:
// Iterate on the 1st lelvel: shoes
foreach ($cart as $shoeId => $sizes) {
// Iterate on the 2nd level: sizes
foreach ($sizes as $sizeId => $quantity) {
$cartWithData[] = [
'shoe' => $shoesRepository->shoeDescr($shoeId),
'shoePrice' => $shoesRepository->find($shoeId),
'quantity' => $quantity,
'size' => $stockRepository->shoeSizeCart($shoeId, $sizeId),
];
}
}
我正在使用 Symfony 中的会话为 PHP 编写鞋店网站的购物车页面。它可以将选定尺寸的商品添加到购物车并计算数量。我需要知道如何使用 foreach 循环显示购物车。
添加商品功能如下:
public function add($shoesid, $sizeid, SessionInterface $session)
{
$cart = $session->get('cart', []);
if(!empty($cart[$shoesid][$sizeid])) {
$cart[$shoesid][$sizeid]++;
} else {
$cart[$shoesid][$sizeid] = 1;
}
$session->set('cart', $cart);
dd($session->get('cart'));
}
这是我添加项目时收到的消息:
> array:2 [▼
40024 => array:1 [▼
410 => 2
]
]
(其中 40024 是 shoesid,410 是 sizeid,2 是数量([shoesid][sizeid] 的数量)
这里有一个显示购物车的函数
public function index(SessionInterface $session, ShoesRepository $shoesRepository, CountriesRepository $countriesRepository, StockRepository $stockRepository)
{
$cart = $session->get('cart', []);
$cartWithData = [];
foreach($cart as $shoesid => [$sizeid => $quantity]) //error here
{
$cartWithData[] = [
'shoe' => $shoesRepository->shoeDescr($shoesid),
'shoePrice' => $shoesRepository->find($shoesid),
'quantity' => $quantity,
'size' => $stockRepository->shoeSizeCart($shoesid, $sizeid)
];
}
$total=0;
$delivery=20;
foreach($cartWithData as $item)
{
$totalItem = $item['shoePrice']->getShoesPrice()*$item['quantity'];
$total += $totalItem;
}
$countries = $countriesRepository->findBy(array(), array('countryname' => 'asc'));
dd($cartWithData);
}
但是当我尝试启动购物车页面时出现下一个错误(第 9 行):
Notice: Undefined variable: sizeid
我应该更改哪些代码才能正确显示项目
$cart
似乎是一个多维数组(确切地说是两个),具有以下结构:
$cart = [
<$shoeId> => [
<$sizeId> => <count>,
<$sizeId> => <count>,
...,
],
<$shoeId> => [
...
],
];
您应该为每个级别设置一个 foreach()
循环来迭代。在这种情况下,您应该有两个:
// Iterate on the 1st lelvel: shoes
foreach ($cart as $shoeId => $sizes) {
// Iterate on the 2nd level: sizes
foreach ($sizes as $sizeId => $quantity) {
$cartWithData[] = [
'shoe' => $shoesRepository->shoeDescr($shoeId),
'shoePrice' => $shoesRepository->find($shoeId),
'quantity' => $quantity,
'size' => $stockRepository->shoeSizeCart($shoeId, $sizeId),
];
}
}