Example PHP shopping cart class Cart.class.php definition and usage

coldplay.xixi
Release: 2023-04-09 10:44:01
forward
2561 people have browsed it

Example PHP shopping cart class Cart.class.php definition and usage

The examples in this article describe the definition and usage of the PHP shopping cart class Cart.class.php. Share it with everyone for your reference, the details are as follows:

The previous developer used JS technology to develop a front-end shopping cart (JS was used for deleting and adding things), but the browser compatibility was not good. Today Finally something went wrong. A foreigner purchased a product. It was possible that he used an unknown browser, chrome, opera... so I had an extra job, rewriting the shopping cart.

No I plan to use JS again and directly consider PHP.

I found a shopping cart class, which is very convenient to use.

Related learning recommendations:PHP programming from entry to proficiency

Cart.class.phpSource code:

<?php
/**
 * Cart
 *
 * 购物车类
 *
 * @author doodoo<pwtitle @yahoo.com.cn="">
 * @package  Cart
 * @category Cart
 * @license  PHP License
 * @access  public
 * @version  $Revision: 1.10 $
 */
Class Cart{
 var $cart;
 var $totalCount; //商品总数量
 var $totalPrices; //商品总金额
 /**
  * Cart Constructor
  *
  * 类的构造函数,使购物车保持稳定的初始化状态
  *
  * @static
  * @access public
  * @return void 无返回值
  * @param void 无参数
  */
 function Cart(){
 $this->totalCount = 0;
 $this->totalPrice = 0;
 $this->cart = array();
 }
 // }}}
 // {{{ add($item)
 /**
 * 增加商品到当前购物车
 *
 * @access public
 * @param array $item 商品信息(一维数组:array(商品ID,商品名称,商品单价,商品数量))
 * @return array 返回当前购物车内商品的数组
 */
 function add($item){
 if(!is_array($item)||is_null($item)) return $this->cart;
 if(!is_numeric(end($item))||(!is_numeric(prev($item)))) {
 echo "价格和数量必须是数字";
 return $this->cart;
 }
 reset($item); //这一句是必须的,因为上面的判断已经移动了数组的指标
 $key = current($item);
 if($key=="") return $this->cart;
 if($this->_isExists($key)){ //商品是否已经存在?
 $this->cart[$key][&#39;count&#39;] = end($item);
 return $this->cart;
 }
 $this->cart[$key][&#39;ID&#39;] = $key;
 $this->cart[$key][&#39;name&#39;] = next($item);
 $this->cart[$key][&#39;price&#39;] = next($item);
 $this->cart[$key][&#39;count&#39;] = next($item);
 return $this->cart;
 }
 // }}}
 // {{{ add($item)
 /**
 * 从当前购物车中取出部分或全部商品
 * 当 $key=="" 的时候,清空当前购物车
 * 当 $key!=""&&$count=="" 的时候,从当前购物车中拣出商品ID号为 $key 的全部商品
 * 当 $key!=""&&$count!="" 的时候,从当前购物车中拣出 $count个 商品ID号为 $key 的商品
 *
 * @access public
 * @param string $key 商品ID
 * @return mixed 返回真假或当前购物车内商品的数组
 */
 function remove($key="",$count=""){
 if($key=="") {
 $this->cart = array();
 return true;
 }
 if(!array_key_exists($key,$this->cart)) return false;
 if($count==""){ //移去这一类商品
 unset($this->cart[$key]);
 }else{ //移去$count个商品
 $this->cart[$key][&#39;count&#39;] -= $count;
 if($this->cart[$key][&#39;count&#39;]<=0) unset($this->cart[$key]);
 }
 return $this->cart;
 }
 // }}}
 // {{{ modi($key,$value)
 /**
 * 修改购物车内商品ID为 $key 的商品的数量为 $value
 *
 * @access public
 * @param string $key 商品ID
 * @param int $value 商品数量
 * @return array 返回当前购物车内商品的数组;
 */
 function modi($key,$value){
 if(!$this->_isExists($key)) return $this->cart(); //不存在此商品,直接返回
 if($value<=0){  // value 太小,全部删除
 unset($this->cart[$key]);
 return $this->cart;
 }
 $this->cart[$key][&#39;count&#39;] = $value;
 return $this->cart;
 }
 /**
 * 返回当前购物车内商品的数组
 *
 * @access public
 * @return array 返回当前购物车内商品的数组;
 */
 function getCart(){
 return $this->cart;
 }
 // }}}
 // {{{ _isExists($key)
 /**
 * 判断当前购物车中是否存在商品ID号为$key的商品
 *
 * @access private
 * @param string $key 商品ID
 * @return bool true or false;
 */
 function _isExists($key)
 {
 if(isset($this->cart[$key])&&!empty($this->cart[$key])&&array_key_exists($key,$this->cart))
 return true;
 return false;
 }
 // }}}
 // {{{ isEmpty()
 /**
 * 判断当前购物车是否为空,即没有任何商品
 *
 * @access public
 * @return bool true or false;
 */
 function isEmpty(){
 return !count($this->cart);
 }
 // }}}
 // {{{ _stat()
 /**
 * 取得部分统计信息
 *
 * @access private
 * @return bool true or false;
 */
 function _stat(){
 if($this->isEmpty()) return false;
 foreach($this->cart as $item){
 $this->totalCount = @end($item);
 $this->totalPrices = @prev($item);
 }
 return true;
 }
 // }}}
 // {{{ totalPrices()
 /**
 * 取得当前购物车所有商品的总金额
 *
 * @access public
 * @return float 返回金额;
 */
 function totalPrices(){
 if($this->_stat())
 return $this->totalPrices;
 return 0;
 }
 // }}}
 // {{{ isEmpty()
 /**
 * 取得当前购物车所有商品的总数量和
 *
 * @access public
 * @return int ;
 */
 function totalCount(){
 if($this->_stat())
 return $this->totalCount;
 return 0;
 }
}//End Class Cart
?>
Copy after login

Methods to use this class:

<?php
header("Content-type:text/html;charset=utf8");
//调用实例
require_once &#39;Cart.class.php&#39;;
session_start();
if(!isset($_SESSION[&#39;cart&#39;])) {
 $_SESSION[&#39;cart&#39;] = new Cart;
}
$cart =& $_SESSION[&#39;cart&#39;];
if( ($_SERVER[&#39;REQUEST_METHOD&#39;]=="POST")&&($_POST[&#39;action&#39;]==&#39;add&#39;) ){
 $p = $_POST[&#39;p&#39;];
 $items = $cart->add($p);
}
if( ($_GET[&#39;action&#39;]==&#39;remove&#39;)&&($_GET[&#39;key&#39;]!="") ) {
 $items = $cart->remove($_GET[&#39;key&#39;]);
}
if( ($_SERVER[&#39;REQUEST_METHOD&#39;]=="POST")&&($_POST[&#39;action&#39;]==&#39;modi&#39;) ){
 $key = $_POST[&#39;key&#39;];
 $value = $_POST[&#39;value&#39;];
 for($i=0;$i<count ($key);$i="" $items="$cart-" ){="">modi($key[$i],$value[$i]);
 }
}
$items = $cart->getCart();
//打印
echo "";
setlocale(LC_MONETARY, &#39;it_IT&#39;);
foreach($items as $item){
 echo "";
 echo "<table border="1"><tbody><tr><form action="\&quot;index.php\&quot;" method="\" post\??=""></form><td>ID:".$item[&#39;ID&#39;]."<input type="hidden" value=".$item[&#39;ID&#39;]." name="key[]">"; echo "</td><td>产品:".$item[&#39;name&#39;]; echo "</td><td>单价:".$item[&#39;price&#39;]; echo "</td><td><input value=".$item[&#39;count&#39;]." name="value[]">"; $sum = $item[&#39;count&#39;]*$item[&#39;price&#39;]; echo "</td><td>合计:".round($sum,2); echo "</td><td><input onclick="\&quot;location=&#39;?action=remove&key=&quot;.$item[&#39;ID&#39;].&quot;&#39;\&quot;" type="button" value="删除">"; } echo "<input type="hidden" value="modi" name="action">"; echo "</td></tr><tr><td colspan="7"><input type="submit" value="提交查询内容">"; echo "</td></tr></tbody></table>";
?>
<hr>
<form action="tmp.php" method="post">
ID:<input name="p[]">
品名:<input name="p[]">
单价:<input name="p[]">
数量:<input name="p[]">
<input type="hidden" value="add" name="action">
<input type="submit" value="提交查询内容">
</form></count>
Copy after login

The above is the detailed content of Example PHP shopping cart class Cart.class.php definition and usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template