Php 调试foreach
我知道这是一个垃圾/基本问题,但我被卡住了,作为一个新手,我需要一些帮助 我试图实现的目标:我希望有一个foreach循环来获取我的用户购物车中的所有产品名称 问题:foreach循环在第二次迭代后停止(如果购物车中有三件东西,我转储,则只显示两件(第一件和第二件)) 我知道foreach循环的作用。我想我的问题在于我的变量名,但我试着去摆弄它们,但没有用Php 调试foreach,php,foreach,Php,Foreach,我知道这是一个垃圾/基本问题,但我被卡住了,作为一个新手,我需要一些帮助 我试图实现的目标:我希望有一个foreach循环来获取我的用户购物车中的所有产品名称 问题:foreach循环在第二次迭代后停止(如果购物车中有三件东西,我转储,则只显示两件(第一件和第二件)) 我知道foreach循环的作用。我想我的问题在于我的变量名,但我试着去摆弄它们,但没有用 if (is_null($cart) || $cart->getSubmitted(true)) { $cart = new
if (is_null($cart) || $cart->getSubmitted(true)) {
$cart = new UserCart();
// since new cart no need to check for duplicate product quantity
// add product to cart
$this->addFlash('notice', 'Creating a new cart because one didnt exist for the user before.');
}
else {
$quantity = new Quantity();
//If the cart is set
$getProductsInCurrentUsersCart = $cart->getQuantities(); //All Products In Users Cart (ARRAY COLLECTION/PERSITENT COLLECTION)
foreach ($getProductsInCurrentUsersCart as $key => $value) {
dump($getProductsInCurrentUsersCart);
$getProductsInCurrentUsersCart = $value->getProduct()->getName(); //SHOULD BE ALL PRODUCTS IN CART
if ($getProductsInCurrentUsersCart === $quantity->setProduct($productBeingAddedToCart)->getProduct()->getName()) {
$this->addFlash('notice', 'Comparission was TRUE.');
$quantity->setQuantity($quantity->getQuantity() + 1);
}
else {
$quantity->setQuantity(1);
$quantity->setProduct($productBeingAddedToCart);
$this->addFlash('notice', 'Comparisson was FALSE.');
} //ENDS IF/ELSE
} //EXECUTING ONCE??????????
$cart->setTimestamp(new \DateTime()); // Set Time Product was Added
// $quantity->setQuantity(1); // Set Quantity Purchased
$cart->setSubmitted(false); // Set Submitted
$cart->setUser($this->getUser()); // Sets the User ONCE
$cart->addQuantity($quantity); // Add Quantity ONCE
$quantity->setUserCart($cart); // Create a UserCart ONCE
$em->persist($productBeingAddedToCart);
$em->persist($cart);
$em->persist($quantity);
$em->flush();
$this->addFlash('notice', 'The product: '.$productBeingAddedToCart->getName().' has been added to the cart!');
}
非常感谢您的帮助
$getProductsInCurrentUsersCart = $value->getProduct()->getName();
在这里,您将重新定义循环中的list变量。尝试使用$getProductsInCurrentUsersCart2,并在此行下方对其进行更改。看看这能不能解决问题。然后想出一个更好的名字:)为什么要在循环中重新定义列表
$getProductsInCurrentUsersCart
?我想这可能就是问题所在。。。