저는 잠시 동안이 문제에 대해 숙고 해 왔으며 실행 방법을 생각할 수 없습니다.항목 속성을 그룹화하는 것은 장바구니입니다
문제점 - 나는 잘 작동하고 ROW (id_cart, id_session, id_product, product_name, qty_product, cart_price, cart_size, cart_variety, date_added)가있는 데이터베이스의 각 항목을 저장하는 장바구니 메커니즘을 구축했습니다. 그러나 현재 id_session 및 id_product가 동일한 테이블에 두 개의 항목이 나타날 때 qty_product, cart_size, cart_variety 및 sub total 행에 두 번째 행을 추가하는 것을 제외하고 product_name을 반복하지 않습니다.
관련 build_cart.php는 다음과 같습니다. 이 총 수량에서 함께 추가됩니다 같은 id_product
및 SUM
와 'your session id'
그룹 중복 행 모든 행을 선택합니다
SELECT
*, SUM(qty_product) AS total_qty
FROM
store_cart
WHERE id_session = 'your session id'
GROUP BY id_product
:
$display_block = '<div id="heading">Your Order</div>';
// Set cart subtotal to zero
$subtotal = 0;
//check for cart items based on user session id
$get_cart_sql = "SELECT * FROM store_cart WHERE id_session = '".$_COOKIE["PHPSESSID"]."'";
$get_cart_res = mysqli_query($dbConnect, $get_cart_sql) or die(mysqli_error($dbConnect));
if (mysqli_num_rows($get_cart_res) < 1) {
//print message
$display_block .= '
<div id="body"><div style="text-align:center; padding:10px; font-weight:bold;">Your cart is currently empty</div>';
} else {
//get info and build cart display
$display_block .= '<div id="body">';
while ($cart_info = mysqli_fetch_array($get_cart_res)) {
$id_cart = $cart_info['id_cart'];
$product_name = stripslashes($cart_info['product_name']);
$product_price = $cart_info['cart_price'];
$qty_product = $cart_info['qty_product'];
$size = $cart_info['cart_size'];
$variety = $cart_info['cart_variety'];
$product_total = sprintf("%.02f", $product_price * $qty_product);
$display_block .= '
<div>
<div id="cart_item">'.$product_name.'</div>
<div id="edit_remove"><a href="cart_remove.php?cartitem='.$id_cart.'">remove</a> | <a href="cart_edit.php?cartitem='.$id_cart.'">edit</a></div>
</div>
<br/>
<br/>
<div>
<div id="qty">'.$qty_product.' X '.$size.'</div>
<div id="cart_price">£'.$product_total.'</div>
</div>
<br/>
<br/>';
$subtotal += $product_total;
}
$display_block .= '</div>'; // Here to close "CART EMPTY div
$display_block .= '
<br/>
<div id="sub_body">
<div>
<div id="sub_titles">Cart Subtotal</div>
<div id="sub_price">£'.$subtotal.'</div>
</div>
<div>
<div id="sub_titles">Delivery</div>
<div id="sub_price">FREE</div>
<br/>
<div>
<div id="sub_titles">VAT</div>
<div id="sub_price">£'.$subtotal*0.2.'</div>
</div>
<div>
<div id="total">Order Total</div>
<div id="total_price">£'.$subtotal*1.2.'</div>
</div>
</div>
<div id="checkout">CHECKOUT</div>';
}
echo $display_block;
큰 응답 @realshadow에 대한
Documentation. 필자는 필요한 결과가있는 해결 방법을 찾아 냈습니다. 내가 스크립트를 다듬을 시간이있을 때 당신의 방법을 구현하는 방법을 살펴볼 것입니다. 카트 메커니즘은 현재 [here] (http://noff-group.com/dev)에 있습니다. 나는 생각에 흥미가있을 것입니다. – Tom
글쎄, 처음에는 SQL 인젝션으로부터 자신을 보호 할 충고를하겠습니다. 예를 들어,이 URL 주소 http://noff-group.com/dev/product_details.php?product=11%20UNION%20SELECT%20username,%20password%20FROM%20USERS에 액세스하면 어떻게되는지 봅니다. 나는 심지어 귀하의 제품 테이블을 떨어 뜨릴 수도 있습니다 :) – realshadow
감사합니다 @ realshadow. 우리가 거기 도착할거야. 비록 일찍부터 눈을 뗄 수있는 좋은 방법 일 것입니다. – Tom