我是一个初学者。当我和zend_session混乱时,我遇到了这样的问题。
只需一件物品,每件事都可以。 但是不止一个项目会停止添加更多项目或增加匹配项目。
这是我的代码
// get value from form
$intQuantity = $this->_request->getPost ( 'intQuantity' );
$intSize = $this->_request->getPost ( 'intSize' );
$intProductId = $this->_request->getParam('productId');
//call model cart
$cartModel = new Default_Model_cart();
//input value into cartModel function addItem output array of matched row
/*
* Array
* (
* [0] => Array
* (
* [product_id] => 7
* [product_price] => 1600000
* [product_image_url_50] => 50_nike-janoski-new-1363618892.jpg
* [product_size_title] => 6.5
* )
* )
*
*
* */
$cartArrays = $cartModel->addItem($intProductId, $intQuantity, $intSize);
//add quantity into cartArrays
/*
* Array
* (
* [0] => Array
* (
* [product_id] => 7
* [product_price] => 1600000
* [product_image_url_50] => 50_nike-janoski-new-1363618892.jpg
* [product_size_title] => 6.5
* [quantity] => 1
* )
* )
*
*
* */
$cartArrays[0]['quantity'] = $intQuantity;
//declare session
$session = new Zend_Session_Namespace();
/*
* if true session->product got at least one array of of cartArray
* */
if(isset($session->product)) {
$i=0;
//foreach arrays into array
foreach($session->product as $productSessionArray) {
/*if in an array got productId and product_size_title matched cartArrays productId and product_size_title
* add value from $intQuantity into appropriate $session->product determined by $i
*/
if($productSessionArray['product_id'] == $cartArrays[0]['product_id'] && $productSessionArray['product_size_title'] == $cartArrays[0]['product_size_title']) {
$session->product[$i]['quantity'] += $intQuantity;
/*
* if values don't matched add the whole new array into $session->product
* */
} else {
$productSessionArrays[] = $productSessionArray;
$productSessionArrays[] = $cartArrays[0];
$session->product = $productSessionArrays;
}
//$i increase by 1
$i++;
}
//if there is not an array in $session added the array in to it
} else {
$session->product = $cartArrays;
}
答案 0 :(得分:0)
看起来你只是测试第一个购物车项目的ID:
if($productSessionArray['product_id'] == $cartArrays[0]['product_id'] && $productSessionArray['product_size_title'] == $cartArrays[0]['product_size_title'])