购物车数量意思就是在我们买东西时可以随时删除工增加商品,这样我们的购物车就必须更新记录啊,下面我来给大家介绍PHP购物车更新购物车数量程序与原因,有需要了解的朋友可参考.
表单部分,代码如下:
- <form action="?action=edit_num" method="post" name="car<?php $c_rs['id'];?>" id="car<?php $c_rs['id'];?>">
- <input name="suliang[<?php echo $c_rs['sp_id'];?>]" type="text" id="suliang[<?php echo $c_rs['sp_id'];?>]" value="<?php echo $c_rs['suliang'];?>"/>
- <input type="submit" name="button" id="button" value="更新购物车" />
- </form>
PHP处理部分,代码如下:
- <?php
- require 'config.inc.php';
- require 'checklogin.php';
- $username = $_SESSION['username'];
- $action = $_GET['action'];
- switch ($action) {
- case "edit_num":
- $arr = $arr = $_POST['suliang'];
- foreach($arr as $key=>$value){
- $sqlgx = "update `cartemp` set suliang='$value' where username='".$username."' and flag=0 and sp_id='".$key."'";
- mysql_query($sqlgx, $conn);
- echo "<script>location.href='shopcat.php'</script>";
- }
- break;
- case "null":
- $null_sql = "delete from `cartemp` where username='$username' and flag=0 ";
- mysql_query($null_sql, $conn);
- echo "<script>location.href='shopcat.php'</script>";
- break;
- case "del":
- $id = $_GET['id'];
- $del_sql = "delete from `cartemp` where id=$id";
- mysql_query($del_sql, $conn);
- echo "<script>location.href='shopcat.php'</script>";
- break;
- }
- ?>
上面全部使用了数据库来操作,下面来个完全的类,代码如下:
- class Cart {
- function check_item( $table, $session, $product) {
-
-
-
- $query = SELECT * FROM $table WHERE session=' $session' AND product=' $product' ;
-
-
-
-
- $result = mysql_query( $query);
- if(! $result) {
- return 0;
- }
-
-
-
- $numRows = mysql_num_rows( $result);
- if( $numRows == 0) {
- return 0;
-
-
-
- } else {
- $row = mysql_fetch_object( $result);
- return $row->quantity;
-
-
-
-
-
-
-
- }
- }
- function add_item( $table, $session, $product, $quantity) {
-
-
-
- $qty = $this->check_item( $table, $session, $product);
-
-
-
- if( $qty == 0) {
- $query = INSERT INTO $table (session, product, quantity) VALUES ;
- $query .= (' $session', ' $product', ' $quantity') ;
- mysql_query( $query);
-
- } else {
- $quantity += $qty;
- $query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' AND ;
- $query .= product=' $product' ;
- mysql_query( $query);
-
-
-
- }
- }
- function delete_item( $table, $session, $product) {
-
-
-
- $query = DELETE FROM $table WHERE session=' $session' AND product=' $product' ;
- mysql_query( $query);
-
-
-
- }
- function modify_quantity( $table, $session, $product, $quantity) {
-
-
-
- $query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' ;
- $query .= AND product=' $product' ;
- mysql_query( $query);
-
-
-
- }
- function clear_cart( $table, $session) {
-
-
-
- $query = DELETE FROM $table WHERE session=' $session' ;
- mysql_query( $query);
- }
- function cart_total( $table, $session) {
-
-
-
- $query = SELECT * FROM $table WHERE session=' $session' ;
- $result = mysql_query( $query);
-
-
-
- if(mysql_num_rows( $result) > 0) {
- while( $row = mysql_fetch_object( $result)) {
-
-
-
- $query = SELECT price FROM inventory WHERE product=' $row->product' ;
- $invResult = mysql_query( $query);
-
-
-
- $row_price = mysql_fetch_object( $invResult);
- $total += ( $row_price->price * $row->quantity);
-
-
-
-
- }
- }
- return $total;
- }
- function display_contents( $table, $session) {
-
-
-
- $count = 0;
-
-
-
-
- $query = SELECT * FROM $table WHERE session=' $session' ORDER BY id ;
- $result = mysql_query( $query);
-
-
-
- while( $row = mysql_fetch_object( $result)) {
-
-
-
- $query = SELECT * FROM inventory WHERE product=' $row->product' ;
- $result_inv = mysql_query( $query);
-
-
-
- $row_inventory = mysql_fetch_object( $result_inv);
- $contents[product][ $count] = $row_inventory->product;
- $contents[price][ $count] = $row_inventory->price;
- $contents[quantity][ $count] = $row->quantity;
- $contents[total][ $count] = ( $row_inventory->price * $row->quantity);
- $contents[description][ $count] = $row_inventory->description;
-
-
-
-
-
-
- $count++;
- }
- $total = $this->cart_total( $table, $session);
- $contents[final] = $total;
-
-
-
-
- return $contents;
-
-
-
- }
- function num_items( $table, $session) {
-
-
-
- $query = SELECT * FROM $table WHERE session=' $session' ;
- $result = mysql_query( $query);
- $num_rows = mysql_num_rows( $result);
- return $num_rows;
-
-
-
- }
- function quant_items( $table, $session) {
-
-
-
- $quant = 0;
- $query = SELECT * FROM $table WHERE session=' $session' ;
- $result = mysql_query( $query);
- while( $row = mysql_fetch_object( $result)) {
-
-
-
- $quant += $row->quantity;
- }
- return $quant;
- }
- }
|