현재 PHP 세션 배열을 사용하여 장바구니를 만들고 있습니다. 나는 순수한 멍청 아. 내가 직면 한 문제는 세션 변수가 그에 따라 업데이트되지 않는다는 것입니다. 동일한 제품이 주어질 때 수량을 증가 시키도록되어있다. 그러나 그렇게되지 않습니다 인 print_r ($ 카트)에 대한쇼핑 카트 용 PHP 세션 배열이 업데이트되지 않습니다.
<?php
session_start();
// get the product id
//$id = isset($_GET['productID']) ;
$pid = $_GET['productID'] ;
/*
* check if the 'cart' session array was created
* if it is NOT, create the 'cart' session array
*/
if(!isset($_SESSION['cart'])){
session_start();
$_SESSION['cart']=array("id","qty");
}
// check if the item is in the array, if it is, do not add
if (in_array($pid, $_SESSION['cart'])){
$cart[$pid]++;
echo "yes";
include "../includes/dbconn.php";
$result=mysql_query("select product_name from mast_product where id=$pid");
$row=mysql_fetch_row($result);
$sizes=sizeof($cart);
print_r($cart);
echo json_encode(array('msg' => 'Success','pname' => $row[0],'total'=> '3'));
}
// else, add the item to the array
else{
$cart[$pid]=1;
echo "No";
include "../includes/dbconn.php";
$result=mysql_query("select product_name from mast_product where id=$pid");
$row=mysql_fetch_row($result);
$sizes=sizeof($cart);
print_r($cart);
echo json_encode(array('msg' => 'Success','pname' => $row[0],'total'=>$cart[$pid]));
}
?>
출력된다 NoArray ([28] => 1) { "MSG": "성공", "PNAME": "HTC 하나" "total": 1}
매번 동일한 출력.
여기서 객체 지향 프로그래밍 방법론으로 옮길 것을 강력하게 제안합니다. 즉, 나는'$ cart '가 초기에 설정되는 곳을 보지 못한다. 또한 $ _SESSION [ 'cart'] = array ("id", "qty");'왜 여기에 id와 qty 항목이 있고 왜 배열에 두 개의 값을 넣을 까? 가치에 대해 키와 수량에 제품 ID를 사용하려고 시도하는 것 같습니까?) –
예. 그렇게하고 싶었습니다. 하지만 PHP 배열의 키와 값에 대한 명확한 개념이 없습니다. –
해결책이 무엇이 될지 알려주시겠습니까 ??? –