2014-01-30 120 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(您的會話增值服務器上的商店)? 正如我可以假設你希望你的客戶在他的瀏覽器(客戶端)上更改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()爲 。 ($ set($ _ POST ['Buy'])) if(isset($ _ POST ['Buy'])) { \t $ pid = $ _ POST ['id']; \t \t $ _SESSION ['cart'] [$ pid] + = 1; header(「location:my_cart.php?msg = Product Added Success」); } – 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(); ($ set($ _ POST ['Buy'])) if(isset($ _ POST ['Buy'])) { \t $ pid = $ _ POST ['id']; \t \t $ _SESSION ['cart'] [$ pid] + = 1; header(「location:my_cart.php?msg = Product Added Success」); }' 但如何更新數量?看,我用過一個表格。但我不確定是否可以通過這種方式更新數量。 – user3252609

+0

@ user3252609你知道thw Ajax嗎?如果是,那麼在每次點擊「購買產品」後,將'$ _SESSION ['cart'] [$ pid]'值更新爲1. Ajax會將產品的ID發送到另一個頁面,如果設置了id,則更新陣列值爲1的relavent ** product_id **索引。 – amey