while 합산
본문
// 예약 상품
$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
// 예약 상품
$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)){ 사용하는 방법 등 다양
strpos(
$row['ct_reserve'], '1')
preg_match
고민해 보세요.
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'));
}
답변을 작성하시기 전에 로그인 해주세요.