成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

php+pdo實現(xiàn)的購物車類代碼分享-創(chuàng)新互聯(lián)

這篇文章主要講解了“php+pdo實現(xiàn)的購物車類代碼分享”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“php+pdo實現(xiàn)的購物車類代碼分享”吧!

我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、鎮(zhèn)賚ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鎮(zhèn)賚網(wǎng)站制作公司

本文實例講述了php+pdo實現(xiàn)的購物車類。分享給大家供大家參考,具體如下:

<?phpsession_start();class Cart{  public $pdo = null;  public function __construct($config)  {    $host = $config['host'];    $user = $config['user'];    $db = $config['db'];    $pwd = $config['pwd'];    if (empty($_SESSION['user_id'])) {      return show(0, '請先登錄');    }    try {      $this->pdo = new PDO("mysql:host=$host;dbname=$db", "$user", "$pwd", array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));      $this->pdo->query("set names utf8");    } catch (PDOException $e) {      echo $e->getMessage();    }  }  //添加商品到購物車  public function add_cart($productid, $num)  {    $sql = "select price from shop_product where id=?";    $stmt = $this->pdo->prepare($sql);    $stmt->execute(array($productid));    $data = $stmt->fetch(PDO::FETCH_ASSOC);    $price = $data['price'];    $createtime = time();    $sql = "select * from shop_cart where productid=? and userid=?";    $stmt = $this->pdo->prepare($sql);    $stmt->execute(array($productid, $_SESSION['user_id']));    $data = $stmt->fetch(PDO::FETCH_ASSOC);    if ($data) {      $sql = "update shop_cart set num=num+? where userid=? and productid=?";      $params = array($num, $_SESSION['user_id'], $productid);    } else {      $sql = "insert into shop_cart(productid,num,userid,price,createtime) values(?,?,?,?,?)";      $params = array($productid, $num, $_SESSION['user_id'], $price, $createtime);    }    $stmt = $this->pdo->prepare($sql);    $stmt->execute($params);    $rows = $stmt->rowCount();    return $rows ?      show(1, 'ok', $rows) :      show(0, 'fail');  }  //修改購買數(shù)量  public function change_num($productid, $num)  {    $sql = "update shop_cart set num=? where userid=? and productid=?";    $stmt = $this->pdo->prepare($sql);    $stmt->execute(array($num, $_SESSION['user_id'], $productid));    $rows = $stmt->rowCount();    return $rows ?      show(1, 'ok', $rows) :      show(0, 'fail');  }  //清空購物車  public function clear_cart()  {    $sql = "delete from shop_cart where userid=?";    $stmt = $this->pdo->prepare($sql);    $this->pdo->execute(array($this->user_id));    $rows = $stmt->rowCount();    return $rows ?      show(1, 'ok', $rows) :      show(0, 'fail');  }  //從購物車中刪除商品  public function remove_cart($productid)  {    $sql = "delete from shop_cart where productid=? and userid=?";    $stmt = $this->pdo->prepare($sql);    $stmt->execute(array($productid, $_SESSION['user_id']));    $rows = $stmt->rowCount();    return $rows ?      show(1, 'ok', $rows) :      show(0, 'fail');  }}//處理數(shù)據(jù)function show($status, $message, $data = array()){  $result = array(    'status' => $status,    'message' => $message,    'data' => $data  );  exit(json_encode($result));}//簡單使用$user = [  'host' => '',  'user' => 'root',  'pwd' => 'root',  'db' => 'shop',];$productid = intval($_POST['productid']);$num = intval($_POST['num']);$cart = new Cart($user);//添加到購物車$cart->add_cart($productid, $num);//刪除指定的商品$cart->remove_cart($productid);//清空$cart->clear_cart();?>

感謝各位的閱讀,以上就是“php+pdo實現(xiàn)的購物車類代碼分享”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對php+pdo實現(xiàn)的購物車類代碼分享這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

網(wǎng)站名稱:php+pdo實現(xiàn)的購物車類代碼分享-創(chuàng)新互聯(lián)
本文URL:http://jinyejixie.com/article34/disspe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、小程序開發(fā)企業(yè)建站、外貿(mào)建站、網(wǎng)站設(shè)計、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
台前县| 冕宁县| 新乡县| 京山县| 沭阳县| 盐源县| 琼中| 皋兰县| 盖州市| 呼伦贝尔市| 商都县| 沽源县| 安国市| 惠水县| 宁明县| 涡阳县| 太谷县| 弥渡县| 滕州市| 郎溪县| 阳原县| 微山县| 广宁县| 铁岭市| 孟州市| 长宁县| 大庆市| 石城县| 威远县| 望都县| 建湖县| 京山县| 高密市| 象州县| 盐津县| 来宾市| 德江县| 来凤县| 资中县| 乳山市| 新民市|