Skip to content

Commit 635c73e

Browse files
committed
sss
1 parent 6696c85 commit 635c73e

File tree

5 files changed

+97
-20
lines changed

5 files changed

+97
-20
lines changed

application/controllers/ProductAPI.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,24 @@ public function ChangeCatgory() {
9696
}
9797

9898

99-
99+
public function GetProducts() {
100+
$productid = $this->input->get_post('list');
101+
//读取model
102+
$pids = array();
103+
$pnum = array();
104+
$this->load->model('Product_model');
105+
if(!empty($productid)){
106+
foreach ($productid as $value){
107+
array_push($pids, $value['pid']);
108+
array_push($pnum, $value['num']);
109+
}
110+
$product_detail = $this->Product_model->get_products($pids, $pnum);
111+
} else {
112+
$product_detail = $this->Product_model->get_products($pids, $pnum);
113+
};
114+
echo json_encode($product_detail);
115+
116+
}
100117

101118
}
102119

application/models/Product_model.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,18 @@ public function change_catgory($catid){
9191
}
9292
}
9393

94-
94+
public function get_products($pids, $pnum){
95+
try{
96+
$Pids = implode(",",$pids);
97+
$sql = "SELECT * FROM products WHERE pid In ($Pids)";
98+
// // use query bindings to prevent against injection.
99+
$query = $this->db->query($sql);
100+
return $query->result();
101+
// var_dump($query->result());
102+
}catch(PDOEXCEPTION $e){
103+
echo $e->getMessage();
104+
}
105+
}
95106

96107

97108
}

application/views/header.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,43 @@
7171
this.checkout();
7272
},
7373
getShoppingCarData:function(){
74+
var that = this;
7475
var shopingList = JSON.parse(localStorage.getItem("shopCar")) || [];
75-
this.showShoppingCar(shopingList);
76+
// console.log(shopingList)
77+
$.ajax({
78+
type: "post",
79+
data: {
80+
list: shopingList
81+
},
82+
url: "./ProductAPI/GetProducts",
83+
dataType: 'json',
84+
success: function(data) {
85+
that.showShoppingCar(data,shopingList);
86+
},
87+
error: function() {
88+
alert("ajax error");
89+
}
90+
});
91+
7692
},
77-
showShoppingCar:function(data){// getShoppingCarData调用 + setShoppingCar调用
93+
showShoppingCar:function(data,numlist){// getShoppingCarData调用 + setShoppingCar调用
7894
if(data.length){
7995
$('.shopping-car').html('');
8096
}else {
8197
$('.shopping-car').html('nothing...');
8298
}
8399

84100
var total = 0;
85-
data.forEach(function(item){
86-
total += Number(item.price)*(item.num);
101+
data.forEach(function(item, i){
102+
total += Number(item.price)*(numlist[i].num);
87103
var shopHtml = `<p data-id="${item.pid}">
88-
<a href="http://github.com/%3Cspan%20class="x x-first x-last">item?id=${item.pid}">${item.name}</a>
89-
<input style="min-width:50px" class="product-num" value="${item.num}" type="number" min="1" max="100"></input>
104+
<a href="http://github.com/%3Cspan%20class="x x-first x-last"> ">${item.name}</a >
105+
<input style="min-width:50px" class="product-num" value="${numlist[i].num}" type="number" min="1" max="100"></input>
90106
<span class="price"> $${item.price}</span>
91-
</p>`
107+
</p >`
92108
$('.shopping-car').append($(shopHtml))
93109
})
94-
$('.total-product').html(total);
110+
$('.total-product').html(parseFloat(total.toPrecision(12)));
95111
},
96112

97113
changeProductNum(){//改变写到locastorage中
@@ -110,10 +126,16 @@ function findProd(prod) {
110126

111127
//动态改价格
112128
var total = 0;
113-
arr.forEach(function(item){
114-
total += Number(item.price)*(item.num);
129+
// arr.forEach(function(item){
130+
// total += Number(item.price)*(item.num);
131+
// })
132+
$('.shopping-car').find('p').each(function(i,e){
133+
var num = $(e).find('input').val()
134+
var price = $(e).find('.price').html().split('$')[1];
135+
total += num * price;
136+
115137
})
116-
$('.total-product').html(total);
138+
$('.total-product').html(parseFloat(total.toPrecision(12)));
117139
})
118140

119141
},
@@ -192,6 +214,7 @@ function findProd(prod) {
192214
total = total + e * price[i];
193215
});
194216
}
217+
total = parseFloat(total.toPrecision(12));
195218
console.log('total',parseFloat(total.toPrecision(12)));
196219
if(response.status == 2){
197220
$('.paypal-form').html(

application/views/home.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@
8888
setShoppingCar: function(data) { //在 addProduct 中调用
8989
var arr = JSON.parse(localStorage.getItem("shopCar")) || [];
9090
var obj = {
91-
name: data.name,
91+
// name: data.name,
9292
num: 1,
9393
pid: data.pid,
94-
price: data.price,
94+
// price: data.price,
9595
}
9696
// 有重复的,num+1;否则扔进arr最后
9797
function findProd(prod) {
@@ -102,8 +102,21 @@ function findProd(prod) {
102102
} else {
103103
arr.push(obj);
104104
}
105-
localStorage.setItem("shopCar", JSON.stringify(arr))
106-
header.showShoppingCar(arr);
105+
localStorage.setItem("shopCar", JSON.stringify(arr));
106+
$.ajax({
107+
type: "post",
108+
data: {
109+
list: arr
110+
},
111+
url: "./ProductAPI/GetProducts",
112+
dataType: 'json',
113+
success: function(data) {
114+
header.showShoppingCar(data,arr);
115+
},
116+
error: function() {
117+
alert("ajax error");
118+
}
119+
});
107120
},
108121

109122
selectCat:function(){

application/views/item.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@
9595
setShoppingCar: function(data) { //在 addProduct 中调用
9696
var arr = JSON.parse(localStorage.getItem("shopCar")) || [];
9797
var obj = {
98-
name: data.name,
98+
// name: data.name,
9999
num: 1,
100100
pid: data.pid,
101-
price: data.price,
101+
// price: data.price,
102102
}
103103
// 有重复的,num+1;否则扔进arr最后
104104
function findProd(prod) {
@@ -110,7 +110,20 @@ function findProd(prod) {
110110
arr.push(obj);
111111
}
112112
localStorage.setItem("shopCar", JSON.stringify(arr))
113-
header.showShoppingCar(arr);
113+
$.ajax({
114+
type: "post",
115+
data: {
116+
list: arr
117+
},
118+
url: "./ProductAPI/GetProducts",
119+
dataType: 'json',
120+
success: function(data) {
121+
header.showShoppingCar(data,arr);
122+
},
123+
error: function() {
124+
alert("ajax error");
125+
}
126+
});
114127
},
115128
getQueryVariable(variable) {
116129
var query = window.location.search.substring(1);

0 commit comments

Comments
 (0)