채택완료

while 합산

2020-09-03 (목) 00:27:09 2,024
Copy
// 예약 상품
            $sql = " select 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);

            while ($row=sql_fetch_array($result)){
echo $row['ct_reserve'];
echo $row['ct_normal'];
                if ($row['ct_reserve'] > '0'  ) {

                    if ($row['ct_normal'] > '0'  ) {

                    alert("예약상품과 일반상품은 같이 주문하실 수 없습니다.");

                    }

                }
            }

일단 $row['ct_reserve']의 합산이 1이상이고 동시에 $row['ct_normal']의 합산이 1이상일 때

알러트가 나오게 하고 싶습니다.

지금 저 소스로는 정상작동은 안되고

echo로 출력하였을 때 장바구니에서 체크된 두 상품의 값은

echo $row['ct_reserve']; -> 10

echo $row['ct_normal']; -> 01

로 나옵니다

각각 하나는 1이고 다른 하나는 0이어서 저렇게 나오는건데요

이걸 어떻게 풀어야 할까요?

합산이 아니라면 ct_reserve 와 ct_normal의 배열 값 중 1이 동시에 포함되어 있을 때는 좀 더 쉬운가요?

답변 4개 / 댓글 6개

채택된 답변
+20 포인트
2020-09-03 (목) 11:31:21
Copy
// 예약 상품
            $sql = " select ct_reserve, ct_normal
                        from {$g5['g5_shop_cart_table']}
                        where od_id = '$tmp_cart_id'
                          and it_id = '$it_id' ";

            $check_reserve = $check_normal = 0; // 합산이라 했으니, 합계를 구할 임시 변수 초기화

            $result = sql_query($sql);
            while ($row=sql_fetch_array($result)){

                $check_reserve+= $row['ct_reserve'];

                $check_normal+= $row['ct_normal'];
            }

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

            // while문 내에서 다른 작업이 필요하다면

            // 배열에 할당해서 반복문 끝나고 그 값을 이용하는 방법

            // 아예 Query로 지정 조건 제외한 선택하는 방법

            // sql_data_seek($result); 추가하고, 다시 while ($row=sql_fetch_array($result)){ 사용하는 방법 등 다양

답변에 대한 댓글 5개

알려주셔서 너무 감사합니다

이해하기 쉽게 주석까지 달아주시고 너무너무 감사합니다
다만 수식은 맞는거 같은데 실제 작동이 안되서

echo $check_normal;
echo $check_reserve;
alert('예약상품과 일반상품은 같이 주문하실 수 없습니다.');

cartupdate.php 부분이라 직접 확인할 수가 없어서

무조건 알러트 뜨게 한 다음에 확인했더니 0과 1이 나오네요

장바구니에 여러 테스트 상품을 지워가면서 확인했는데

장바구니를 비우고 첫번째 담는 상품의 정보가 표시되는거 같아요

1 0 인 상품을 처음 담으면 1 0 이 나와요

그 뒤로는 다른 상품을 담아도 바뀌지 않구요

아래쪽 다른 답변에 db 사진도 올렸습니다 ㅠㅠ
이 배열이 안되는? 문제는 계속 있었던거 같은데

다른 수식을 썼을 때도 어쩔 때는 저렇게 하나만 나오는데

또 어떨때는 2개 나오고 이상합니다.

특별하게 만진게 없고 cartupdate.php의 if($act == "buy") 부분의

// 주문 상품의 재고체크 부분 끝나고 뒤쪽에다 추가한건데 미치겠네요 ㅠㅠ
아이고;;; 수정했던 것 같은데;;; 코드에 오류 있어요.
if ( $check_reserve && $check_normal ) 부분에 $ 빠졌었습니다.
[code]
// 예약 상품
$sql = " select ct_reserve, ct_normal
from {$g5['g5_shop_cart_table']}
where od_id = '$tmp_cart_id'
and it_id = '$it_id' ";
$check_reserve = $check_normal = 0; // 합산이라 했으니, 합계를 구할 임시 변수 초기화
$result = sql_query($sql);
while ($row=sql_fetch_array($result)){
$check_reserve+= $row['ct_reserve'];
$check_normal+= $row['ct_normal'];
}
if ( $check_reserve && $check_normal ) alert('예약상품과 일반상품은 같이 주문하실 수 없습니다.');
// while문 내에서 다른 작업이 필요하다면
// 배열에 할당해서 반복문 끝나고 그 값을 이용하는 방법
// 아예 Query로 지정 조건 제외한 선택하는 방법
// sql_data_seek($result); 추가하고, 다시 while ($row=sql_fetch_array($result)){ 사용하는 방법 등 다양
[/code]
네 그건 제가 수정해서 적용했었어요 ㅠㅠ

흑흑

for 구문으로 변경하고 k값을 구해도 1만 나오네요

기존 있던 재고체크 쪽은 잘되는데 왜 안되는거지 미치겠네요
2020-09-03 (목) 15:36:56
Copy
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
                        from {$g5['g5_shop_cart_table']}
                        where od_id = '$tmp_cart_id'
                          and it_id = '$it_id' ";
            $check_reserve = $check_normal = 0; // 합산이라 했으니, 합계를 구할 임시 변수 초기화
            $result = sql_query($sql);
            while ($row=sql_fetch_array($result)){
                $check_reserve+= $row['ct_reserve'];
                $check_normal+= $row['ct_normal'];
            }

            echo $check_normal;
            echo $check_reserve;
            if ( $check_reserve && $check_normal ) alert('예약상품과 일반상품은 같이 주문하실 수 없습니다.');
            // while문 내에서 다른 작업이 필요하다면
            // 배열에 할당해서 반복문 끝나고 그 값을 이용하는 방법
            // 아예 Query로 지정 조건 제외한 선택하는 방법
            // sql_data_seek($result); 추가하고, 다시 while ($row=sql_fetch_array($result)){ 사용하는 방법 등 다양

            $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'));
}
2020-09-03 (목) 13:40:11

990320945_1599108010.2825.jpg

실제 장바구니 테이블엔 저렇게 들어가 있습니다

2020-09-03 (목) 01:06:31
strpos($row['ct_reserve'], '1')
preg_match

고민해 보세요.

답변에 대한 댓글 1개

감사합니다 ㅠㅠ 덕분에 많이 공부했어요 정말 감사합니다

답변을 작성하려면 로그인이 필요합니다.