欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長(zhǎng)資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          php用session實(shí)現(xiàn)購(gòu)物車的方法

          php用session實(shí)現(xiàn)購(gòu)物車的方法:首先創(chuàng)建前端代碼實(shí)現(xiàn)加入購(gòu)物車動(dòng)作;然后在PHP文件中實(shí)現(xiàn)購(gòu)物車數(shù)據(jù)的邏輯判斷;接著根據(jù)數(shù)據(jù)加載列表;最后通過(guò)unset等函數(shù)實(shí)現(xiàn)商品的刪除或金額計(jì)算即可。

          php用session實(shí)現(xiàn)購(gòu)物車的方法

          推薦:《PHP視頻教程》

          PHP 使用session實(shí)現(xiàn)購(gòu)物車效果(點(diǎn)擊按鈕添加數(shù)據(jù),根據(jù)數(shù)據(jù)生成列表,刪除選中的商品,點(diǎn)擊按鈕時(shí)計(jì)算金額)

          效果圖

          php用session實(shí)現(xiàn)購(gòu)物車的方法php用session實(shí)現(xiàn)購(gòu)物車的方法

          代碼很多,截取一部分

          加入購(gòu)物車(點(diǎn)擊按鈕添加session數(shù)據(jù))

          php用session實(shí)現(xiàn)購(gòu)物車的方法

              // 加入購(gòu)物車     function addshopping() {       <?php       $db = db($_GET['db']);       $id = $formatData['id'];       $user = $formatData['user'];       // 當(dāng)購(gòu)物車沒(méi)有時(shí),即沒(méi)有session數(shù)據(jù)時(shí)       if (empty($_SESSION['gwc'])) {         $arr = array(array('user' => $user, 'db' => $db, 'id' => $id, 'num' => 1));         $_SESSION['gwc'] = $arr;       } else {         // 當(dāng)購(gòu)物車有數(shù)據(jù)時(shí)         $arr = $_SESSION["gwc"];         $order = false;         foreach ($arr as $key => $value) {           // 如果數(shù)據(jù)中有了這個(gè)數(shù)據(jù)           if ($value['user'] == $user && $value['db'] == $db && $value['id'] == $id) {             $index = $key;  //保存這個(gè)相同數(shù)據(jù)的下標(biāo),以便操作             $order = true;	             //定義一個(gè)變量,看是佛有相同的數(shù)據(jù),需要注意的是,不能再這個(gè)if語(yǔ)句中else,否則后面的語(yǔ)句             //雖然會(huì)使數(shù)量加一,但是還是會(huì)多創(chuàng)建一條數(shù)據(jù),為什么,自己想想就能明白,只是當(dāng)時(shí)當(dāng)局者迷           }         }         if ($order) {           // 如果該商品在購(gòu)物車存在,讓數(shù)量加1           $arr[$index]['num']++;         } else {           // 如果該商品購(gòu)物車?yán)锩娌淮嬖?,造一個(gè)一維數(shù)組扔到二維里面           $arr[] = array('user' => $user, 'db' => $db, 'id' => $id, 'num' => 1);         }         $_SESSION["gwc"] = $arr;       }        ?>       console.log('addshopping');     }

          根據(jù)數(shù)據(jù)加載列表

              <!-- 購(gòu)物車列表 -->     <?php     foreach ($_SESSION['gwc'] as  $dataList) {         $clean = array();         $clean['user'] = $dataList['user'];         $clean['db'] = $dataList['db'];         $clean['id'] = $dataList['id'];         $clean['num'] = $dataList['num'];          $res = $conn->fetchRow("SELECT * FROM {$clean['db']}  WHERE id={$clean['id']}");         $clean['title'] = $res['title'];         $clean['price'] = $res['price'];         $clean['face']  = $res['face'];      ?>         <p class="weui-content">             <p class="weui-panel weui-panel_access">                  <p class="weui-panel__hd"><span><?php echo  $clean['user']; ?> </span><a href="javascript:void(0)" class="wy-dele"><!-- 刪除 --></a></p>                  <p class="weui-panel__bd">                     <p class="weui-media-box_appmsg pd-10">                         <p class="weui-media-box__hd check-w weui-cells_checkbox">                             <label class="weui-check__label" for="cart-<?php echo $clean['db'] . '-' . $clean['id'] ?>">                                                       <p class="weui-cell__hd cat-check">                                     <input type="checkbox" class="weui-check" name="cartpro" id="cart-<?php echo $clean['db'] . '-' . $clean['id'] ?>"><i class="weui-icon-checked"></i>                                 </p>                             </label>                         </p>                         <p class="weui-media-box__hd"><a href="pro_info.php?db=<?php echo  $clean['db']; ?>&id=<?php echo  $clean['id']; ?>"><img class="weui-media-box__thumb" src="<?php echo  $clean['face'];  ?>" alt="" style="height: 100%"></a></p>                         <p class="weui-media-box__bd">                             <h1 class="weui-media-box__desc"><a href="pro_info.php?db=<?php echo  $clean['db']; ?>&id=<?php echo  $clean['id']; ?>" class="ord-pro-link"><?php echo  $clean['title'];  ?></a></h1>                             <p class="weui-media-box__desc">規(guī)格:<span>紅色</span>,<span>23</span></p>                             <p class="clear mg-t-10">                                 <p class="wy-pro-pri fl">¥<em class="num font-15 price"><?php echo  $clean['price'];  ?></em></p>                                 <p class="pro-amount fr">                                     <!-- 購(gòu)買的數(shù)量 -->                                     <input type="hidden" name="" class="datanum" value="<?php echo  $clean['num']; ?>">                                     <!-- 提交刪除的數(shù)據(jù) -->                                     <input type="hidden" name="" class="rmdata" value="shopping.php?db=<?php echo  $clean['db']; ?>&id=<?php echo  $clean['id'];?>">                                     <p class="Spinner"></p>                                 </p>                             </p>                         </p>                     </p>                 </p>               </p>         </p>     <?php }     ?>

          刪除選中的商品

          // echo '<pre>';// print_r($_SESSION['gwc']);// echo '</pre>';if(isset($_GET['db']) && isset($_GET['id'])){     $dataArr = $_SESSION['gwc'];     foreach ($dataArr as $key => $value) {         if($value['db'] == $_GET['db'] && $value['id'] == $_GET['id']){             unset($dataArr[$key]);         }     }     $_SESSION['gwc'] = $dataArr;     header("Location:shopping.php");}// echo '<pre>';// print_r( $dataArr);// echo '</pre>';

          金額計(jì)算

              <!-- 金額計(jì)算 -->     <script>         window.onload = function() {             var cartproAll = document.querySelectorAll('input[name=cartpro]');	             var datanum = document.querySelectorAll('.datanum');                var numAll = document.querySelectorAll('.Amount');             var DisDe  = document.querySelectorAll('.DisDe');             var Increase  = document.querySelectorAll('.Increase');              //  購(gòu)買數(shù)量的多少             numAll.forEach((item,i) => {                 item.value = datanum[i].value;             })              // 點(diǎn)擊加號(hào)             DisDe.forEach((item, i) => {                 item.onclick = function() {                     computedPrice();                 }             })             // 點(diǎn)擊減號(hào)             Increase.forEach((item, i) => {                 item.onclick = function() {                     computedPrice();                 }             })             // 點(diǎn)擊單選時(shí)計(jì)算金額             cartproAll.forEach((item, i) => {                 item.onclick = function() {                     computedPrice();                 }             })         }         function computedPrice(){             var cartproAll = document.querySelectorAll('input[name=cartpro]');      //獲取是否點(diǎn)擊             var price = document.querySelectorAll('.price');                        //價(jià)錢             var price_total = document.querySelector('.price_total');               //價(jià)錢總數(shù)                var numAll = document.querySelectorAll('.Amount');                      //數(shù)量             var total = 0;             price_total.innerHTML = 0;             cartproAll.forEach((item, i) => {                 if (item.checked == true) {                         total += parseFloat(price[i].innerHTML) * parseFloat(numAll[i].value);                     }             })             price_total.innerHTML = total;         }     </script>

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)