php物件導向--實現購物車類(session),車類session

來源:互聯網
上載者:User

php物件導向--實現購物車類(session),車類session
首先需要有購物車類:

<?phpclass car{function car(){@session_start();//開啟session}function addItem( $key,$object ){$_SESSION['list'][$key] = $object;//增加}function getItem(){return $_SESSION['list'];//擷取購物車的資訊}function rmItem( $key ){unset( $_SESSION['list'][$key] );//清除物品}}

然後建立一個產品類:

<?phpclass products{function products(){@session_start();}function addProducts( $key,$name,$price,$pic ){$data = array("name"=>$name,"price"=>$price,"pic"=>$pic,);$_SESSION['products'][$key] = $data;}function getProducts(){return $_SESSION['products'];}}


對購物車物品的增加:

<?phperror_reporting(0);//清除warning 和notice@session_start();include 'car.class.php';$key = $_GET['key'];$book = $_SESSION['products'][$key];//print_r( $data );$car = new car();if( $book ){$car -> addItem( $key,$book );}$data = $car->getItem();echo "<table style='font-size:11px;font-family:verdana;width:550px;text-align:center;padding:10px;'>";$total = 0;foreach( $data as $key=>$rs){echo "<tr>";echo "<td style='border-bottom:2px solid #ccc;'><img src='".$rs['pic']."' height='96px' width='94px' /></td>";//物品圖片echo "<td style='border-bottom:2px solid #ccc;'>".htmlspecialchars($rs['name'])."</td>";//物品名echo "<td style='border-bottom:2px solid #ccc;'>".$rs['price']."</td>";//價格echo "<td style='border-bottom:2px solid #ccc;'><a href='rm.php?key=$key'>Remove</a></td>";//清除按鈕echo "</tr>";$total += $rs['price'];//總價顯示}echo "<tr><td style='color:red;font-weight:bold;'>Total Money:</td><td>-</td><td style='color:blue;font-weight:bold;'>{$total}</td><td><a href='index.php'>back to add</a></td></tr>";echo "</table>";

index.php可自行操作。

購物車的物品增加由於數量無設定,所以最多達到所有物品全存在。金錢等於全部總和,再次增加也不會有任何變化




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.