2014-01-30 2 views
-1

첫 번째 장바구니 프로젝트를 만드는 중입니다.하지만 제품 수량을 업데이트하는 동안 멈추었습니다.PHP 장바구니 : 세션 변수를 사용하여 장바구니에서 제품 수량을 업데이트하는 방법은 무엇입니까?

세션 변수를 사용하여 제품 수량을 업데이트하고 싶습니다. 누구든지 코드를 알려주고 그 코드에 대해 어떻게 설명 할 수 있습니까? 여기

가 my_cart.php 페이지 코드

<tr> 
     <th class="tablerow">Product Code</th> 
     <th class="tablerow">Product Name</th> 
     <th class="tablerow">Image</th> 
     <th class="tablerow">Quantity</th> 
     <th class="tablerow">Price</th> 
     <th class="tablerow">Total Price</th> 
     <th class="tablerow">Action</th>     
    </tr> 

    <?php 
    $grand_total = 0;         // For Calculating Grand Price 
    foreach($_SESSION['cart'] as $id=>$quantity) 
    { 
     $sql="SELECT * FROM products WHERE id='$id'"; 
     $result=mysql_query($sql) or die("Error Message"); 
     while($row=mysql_fetch_array($result)) 
     {  
     $grand_total+= $row['product_price']*$quantity; // For Calculating Grand Price 
    ?>   

    <tr>     
     <td class="tablerow"><?php echo $row['id']; ?></td> 
     <td class="tablerow"><?php echo $row['product_name']; ?></td> 
     <td class="tablerow"><?php echo "<img height='50' width='50' src='admin/".$row['product_image']."'/>" ?></td> 

     <form name="update_cart" action="update_cart.php" method="post"> 

     <td class="tablerow"><input type="text" name="quantity" value="<?php echo $quantity; ?>" /><br /><input type="image" src="admin/images/updatecart.png" name="Update" value="Update" /></td> 

     </form> 

     <td class="tablerow"><?php echo $row['product_price']; ?></td> 
     <td class="tablerow"><?php echo $quantity*$row['product_price']; ?> </td> 
     <td class="tablerow"><?php print "<a href='delete_cart.php?pid=".$row['id']."'><img src='admin/images/delete.png'></a>"; ?></td></form> 
    </tr> 

    <?php 
     } 
    } 
    ?> 

    <tr> 
     <td class="tablerow" colspan="7">Grand Total: Rs <?php echo $grand_total; ?></td> 
    </tr>  

    <tr> 
     <td class="tablerow" colspan="7"><?php print "<a href='clear_cart.php'><img src='admin/images/clearcart.png'></a><a href='http://localhost/Shopping-Cart/front-end/'><img src='admin/images/continueshopping.png'></a><a href='update_cart.php'><img src='admin/images/placeorder.png'></a>";?></td> 
    </tr>    

    </table> 

답변

0

내가 세션 변수

이를 사용하여 제품의 수량을 업데이트 할 수있는 질문은 당신이 업데이 트하려는 곳에서 클라이언트 또는 서버에서만 qnt (세션 vars가 서버에 저장 됨)? 브라우저에서 (고객의) 고객의 변경 사항을 qnt하고 서버가 알고 있어야한다고 가정 할 수 있습니까? 그렇다면 클라이언트에서 JS-Ajax 요청 (Jquery 더 쉬운 방법)을 사용하여 파일에 ajax 요청을 보내야합니다 (예 : change_qnt.php). 성공하면 클라이언트 (브라우저)에서 qnt가 업데이트됩니다.

change_qnt.php qnt를 저장 한 세션 변수를 변경하고 방문자 페이지에서 qnt를 업데이트했는지 여부에 따라 성공 또는 실패하면 결과를 반환합니다.

아주 간단한 예 : 세션 배열의

<script> 
$('selector_that_will_change_qnt').click() { 
.ajax ({ 
url: 'change_qnt.php', 
type: 'post', 
dataType: 'html', 
success: function() { // code here if success request}, 
error: function(){ //code here if request was failed} 
}); 
} 
</script> 
+0

세션 변수를 사용하는 클라이언트 측. 다음은 add_cart.php 페이지 코드입니다. session_start(); if (isset ($ _ POST [ 'Buy'])) { \t $ pid = $ _ POST [ 'id']; \t \t $ _SESSION [ 'cart'] [$ pid] + = 1; 헤더 ("location : my_cart.php? msg = 제품이 성공적으로 추가되었습니다"); } – user3252609

0

저장 모든 제품 ID. 색인은 제품 id와 같아야합니다. 즉, 사용자가 새 제품을 클릭하거나 추가 한 다음 배열의 $_SESSION['cart']['product_id'] 위치에 1을 저장하면됩니다. 사용자가 동일한 제품을 다시 추가하면 $_SESSION['cart']['product_id']++을 수행 한 다음 foreach($_SESSION['cart'] as $id=>$quantity)을 사용하여 배열을 인쇄 할 수 있습니다. 위의 개념을 쉽게 코딩 할 수 있습니다.

여기에서 무엇인가를 이해할 수 있다면 예제가 있습니다.

if(isset(isset($_REQUEST['pid']) && !empty($_REQUEST['pid'])) { 
    $_SESSION['item'] = $_SESSION['item'] + 1; //Total number of items in cart 
    $p_id = $_REQUEST['pid']; //Clicked product's id 
    $_SESSION['cart'][$p_id]++; //Increment in relevant index value by one 
    $cost_query = "select * from product where id=$p_id"; 
    //Calculate cost here and store it in session variable to use in your main page or where you are displaying your cart 
} 
+0

이미 add_cart.php 페이지에서이 작업을 수행했습니다. 다음은 코드 'session_start(); if (isset ($ _ POST [ 'Buy'])) { \t $ pid = $ _ POST [ 'id']; \t \t $ _SESSION [ 'cart'] [$ pid] + = 1; 헤더 ("location : my_cart.php? msg = 제품이 성공적으로 추가되었습니다"); } ' 수량을 업데이트하는 방법? 봐, 나는 양식을 사용했다. 그러나이 방법으로 수량을 업데이트 할 수 있는지 확실하지 않습니다. – user3252609

+0

@ user3252609 아약스를 아십니까? 그렇다면 "제품 구매"를 클릭 할 때마다 '$ _SESSION ['cart '] [$ pid]'값을 1 씩 갱신합니다. Ajax는 제품의 ID를 다른 페이지로 보내고 id가 설정된 경우 배열을 업데이트합니다 relavent ** product_id ** 색인의 값은 1입니다. – amey

관련 문제