導讀 跟大家講解下有關淘寶怎么加入購物車 php 購物車實例(申精),相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說淘寶怎么加
跟大家講解下有關淘寶怎么加入購物車 php 購物車實例(申精),相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說淘寶怎么加入購物車 php 購物車實例(申精),小編也收集到了有關淘寶怎么加入購物車 php 購物車實例(申精)的相關資料,希望大家看到了會喜歡。
if(! $session && ! $scid) { $session = md5(uniqid(rand())); SetCookie(scid, $session, time() + 14400); } 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; //返回總量 } }以上就介紹了淘寶怎么加入購物車 php 購物車實例(申精),包括了淘寶怎么加入購物車方面的內容,希望對PHP教程有興趣的朋友有所幫助。
來源:php中文網