지금 장바구니 전체를 불러오는데 체크된 값만 가져오게

지금 장바구니 전체를 불러오는데 체크된 값만 가져오게

QA

지금 장바구니 전체를 불러오는데 체크된 값만 가져오게

본문


// 예약 상품
$sql = " select ct_reserve, ct_normal, it_id
  from {$g5['g5_shop_cart_table']}
  where od_id = '$tmp_cart_id' ";
$result = sql_query($sql);
 
while ($row=sql_fetch_array($result)) {
$sql = " select SUM(ct_reserve) as reserve, SUM(ct_normal) as normal
            from {$g5['g5_shop_cart_table']}
            where od_id = '$tmp_cart_id'
              and it_id = '{$row['it_id']}' ";
$sum = sql_fetch($sql);

$tot_reserve+= $sum['reserve'];
$tot_normal+= $sum['normal'];
}

if ( $tot_normal > 0 && $tot_reserve > 0 ) alert('예약상품과 일반상품은 같이 주문하실 수 없습니다.');

 

이렇게 하니까 각 값이 합산이 되긴 하는데

 

장바구니에서 전체값을 다 불러오네요

 

체크된 값만 불러오게 할 수 있나요?

 

해당 액션 전문입니다

 


if($act == "buy")
{
    if(!count($_POST['ct_chk']))
        alert("주문하실 상품을 하나이상 선택해 주십시오.");
    // 선택필드 초기화
    $sql = " update {$g5['g5_shop_cart_table']} set ct_select = '0' where od_id = '$tmp_cart_id' ";
    sql_query($sql);
    $fldcnt = count($_POST['it_id']);
    for($i=0; $i<$fldcnt; $i++) {
        $ct_chk = $_POST['ct_chk'][$i];
        if($ct_chk) {
            $it_id = $_POST['it_id'][$i];
            // 본인인증, 성인인증체크
            if(!$is_admin) {
                $msg = shop_member_cert_check($it_id, 'item');
                if($msg)
                    alert($msg, G5_SHOP_URL);
            }
            // 주문 상품의 재고체크
            $sql = " select ct_qty, it_name, ct_option, io_id, io_type, ct_reserve, ct_normal
                        from {$g5['g5_shop_cart_table']}
                        where od_id = '$tmp_cart_id'
                          and it_id = '$it_id' ";
            $result = sql_query($sql);
            for($k=0; $row=sql_fetch_array($result); $k++) {
                $sql = " select SUM(ct_qty) as cnt from {$g5['g5_shop_cart_table']}
                          where od_id <> '$tmp_cart_id'
                            and it_id = '$it_id'
                            and io_id = '{$row['io_id']}'
                            and io_type = '{$row['io_type']}'
                            and ct_stock_use = 0
                            and ct_status = '쇼핑'
                            and ct_select = '1' ";
                $sum = sql_fetch($sql);
                $sum_qty = $sum['cnt'];
                // 재고 구함
                $ct_qty = $row['ct_qty'];
                if(!$row['io_id'])
                    $it_stock_qty = get_it_stock_qty($it_id);
                else
                    $it_stock_qty = get_option_stock_qty($it_id, $row['io_id'], $row['io_type']);
                if ($ct_qty + $sum_qty > $it_stock_qty)
                {
                    $item_option = $row['it_name'];
                    if($row['io_id'])
                        $item_option .= '('.$row['ct_option'].')';
                    alert($item_option." 의 재고수량이 부족합니다.\\n\\n현재 재고수량 : " . number_format($it_stock_qty - $sum_qty) . " 개");
                }
            }
            //장바구니 품절
            $sql = " select b.it_soldout
                                  from {$g5['g5_shop_cart_table']} a left join {$g5['g5_shop_item_table']} b on ( a.it_id = b.it_id )
                                  where a.od_id = '$tmp_cart_id'
                                  and a.it_id = '$it_id' ";
            $res = sql_query($sql);
            while ($row=sql_fetch_array($res)){
                if ($row['it_soldout'])
                {
                    alert("주문하시려는 상품 중 품절인 상품이 있습니다.");
                }
            }
            // 예약 상품
            $sql = " select ct_reserve, ct_normal, it_id
              from {$g5['g5_shop_cart_table']}
              where od_id = '$tmp_cart_id' ";
            $result = sql_query($sql);
 
            while ($row=sql_fetch_array($result)) {
            $sql = " select SUM(ct_reserve) as reserve, SUM(ct_normal) as normal
                        from {$g5['g5_shop_cart_table']}
                        where od_id = '$tmp_cart_id'
                          and it_id = '{$row['it_id']}' ";
            $sum = sql_fetch($sql);

            $tot_reserve+= $sum['reserve'];
            $tot_normal+= $sum['normal'];
            }
 
            if ( $tot_normal > 0 && $tot_reserve > 0 ) alert('예약상품과 일반상품은 같이 주문하실 수 없습니다.');
 
            $sql = " update {$g5['g5_shop_cart_table']}
                        set ct_select = '1',
                            ct_select_time = '".G5_TIME_YMDHIS."'
                        where od_id = '$tmp_cart_id'
                          and it_id = '$it_id' ";
            sql_query($sql);
        }
    }
    if ($is_member) // 회원인 경우
        goto_url(G5_SHOP_URL.'/orderform.php');
    else
        goto_url(G5_BBS_URL.'/login.php?url='.urlencode(G5_SHOP_URL.'/orderform.php'));
}

 

이 질문에 댓글 쓰기 :

답변 2

체크된값만 가져오는거면 예약상품 쿼리에 and it_id = '$it_id' 다시 넣어야할것같네요

몇번보다니보니 체크된상품에 예약상품 여부를 확인할려고하시는것같은데 굳이 쿼리를 나누기보다는 재고체크할때 같이 들어가도 될것같네요

예약상품이면 ct_reserve = 1 인거죠? 재고검사할때 예약상품인지 체크하고 예약상품이면 $tot_reserve++;로 처리를해줄수있을것같네요

 

사실 원래 예약상품은 it_2 = 1 이고 아닌 상품은 it_2 =''

인데 제가 부족하다보니 많은 도움을 받는 과정에서

it_2로 처리하는거보다 값을 따로 넣어서 하는게 더 편하겠다 생각해서

이렇게 변형된거에요 ㅎㅎ 진짜 3일 동안 꼬박 이것만 붙잡았네요 ㅠㅠ

쿼리문은 최대한 줄이는게 퍼포먼스에 좋으니
어차피 체크된 상품을 재고검사할때 한번씩 확인하잖아요 여기서
//주문 상품의 재고체크 밑에 쿼리에서 ct_reserve, ct_normal값을 가져올수있도록 추가하고
그다음 for문에서
if($row['ct_reserve']) $tot_reserve++;
else if($row['ct_noraml']) $tot_normal++;
이런식으로 해당 상품일경우 체크합니다
물론 for문 들어가기 이전에 $tot_reserve = $tot_normal = 0; 선언해주시구요

 그런다음 for문 빠져나왔을때(재고체크가 끝났을떄)
if ( $tot_normal > 0 && $tot_reserve > 0 ) alert('예약상품과 일반상품은 같이 주문하실 수 없습니다.');
얼럿을 추가해주시면 간단하게 쿼리문 두개나 추가안하여도 체크가 가능하죠


            // 주문 상품의 재고체크
            $sql = " select ct_qty, it_name, ct_option, io_id, io_type, ct_normal, ct_reserve
                        from {$g5['g5_shop_cart_table']}
                        where od_id = '$tmp_cart_id'
                          and it_id = '$it_id' ";
            $result = sql_query($sql);
            $tot_reserve = $tot_normal = 0; 
            for($k=0; $row=sql_fetch_array($result); $k++) {
                $sql = " select SUM(ct_qty) as cnt from {$g5['g5_shop_cart_table']}
                          where od_id <> '$tmp_cart_id'
                            and it_id = '$it_id'
                            and io_id = '{$row['io_id']}'
                            and io_type = '{$row['io_type']}'
                            and ct_stock_use = 0
                            and ct_status = '쇼핑'
                            and ct_select = '1' ";
                $sum = sql_fetch($sql);
                $sum_qty = $sum['cnt'];
                if($row['ct_reserve']) $tot_reserve++;
                else if($row['ct_normal']) $tot_normal++;
                // 재고 구함
                $ct_qty = $row['ct_qty'];
                if(!$row['io_id'])
                    $it_stock_qty = get_it_stock_qty($it_id);
                else
                    $it_stock_qty = get_option_stock_qty($it_id, $row['io_id'], $row['io_type']);
                if ($ct_qty + $sum_qty > $it_stock_qty)
                {
                    $item_option = $row['it_name'];
                    if($row['io_id'])
                        $item_option .= '('.$row['ct_option'].')';
                    alert($item_option." 의 재고수량이 부족합니다.\\n\\n현재 재고수량 : " . number_format($it_stock_qty - $sum_qty) . " 개");
                }
            }
            if ( $tot_normal > 0 && $tot_reserve > 0 ) alert('예약상품과 일반상품은 같이 주문하실 수 없습니다.');

이상하네요 딱맞는데 ㅎㅎ 음 기존에 sum하신거보면 ct_resrve , ct_normal이 처리가 어떻게 되어있는걸까요?
해당 코드만으로 판단하기엔 sum부분 쿼리문은 굳이 필요없다 판단했는데 아닌가보네요

답변을 작성하시기 전에 로그인 해주세요.
전체 16,776
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT