Invalid argument supplied for foreach() in 에러

Invalid argument supplied for foreach() in 에러

QA

Invalid argument supplied for foreach() in 에러

본문

foreach($DATA['cart'] as $key=>$val) {
        $this_price = ($val['ct_price']+$val['io_price']) * $val['ct_qty'];
        $total_price += $this_price;

 

이 구문에서 

 

Warning: Invalid argument supplied for foreach() in /www_root/shop/cartprint.php on line 196

 

라는 메세지가 뜨는데 무슨 에러일까요 ㅠㅠ?

이 질문에 댓글 쓰기 :

답변 4

php 버전과 영카트 버전이 어떻게 되지요? 

 


    <?php
    $total_price = 0;
    $this_price = 0;
    if (isset($DATA['cart']) && is_array($DATA['cart'])) {
    foreach($DATA['cart'] as $key=>$val) {
        $this_price = ($val['ct_price']+$val['io_price']) * $val['ct_qty'];
        $total_price += $this_price;
        
    ?>
    <tr>
        <td class="td-body"><?php $val['io_id']?></td>
        <td class="td-body"><?php $val['it_name']?></td>
        <td class="td-body"><?php $DATA['opt'][$key]['io_misc01']?></td>
        <td class="td-body"><?php $DATA['opt'][$key]['io_misc02']?></td>
        <td class="td-body td-right"><?=number_format($val['ct_price']+$val['io_price'])?></td>
        <td class="td-body td-center"><?=number_format($val['ct_qty'])?></td>
        <td class="td-body td-right"><?=number_format($this_price)?></td>
    </tr>
        
    <?
    } # end of if 
    } # end foreach;
    ?>

$DATA['cart'] 가 null 인 것 같네요. 
if 문을 추가해야할 듯요

if (isset($DATA['cart']) && is_array($DATA['cart'])) {
}

print_r($_GET);
print_r($_POST);

이렇게 해서 출력이 되나 확인을 해보았는데 Array ( ) Array ( )

이렇게 뜨네요  ㅠㅠㅠ 값을 불러오지 못하는듯 한데.... 코드좀 봐주실 수 있으실까요 ㅠㅠ?

<?php
include_once('./_common.php');
include_once(G5_PATH.'/head.sub.php');

print_r($_GET);
print_r($_POST);
# 세션데이터에서 장바구니 전송값을 가져온다. ---------------------------------------------------
# [cart.php -> ajax.cartprint.php 세션저장한 값]
$tmp_data = get_session("cartprint");
if($tmp_data) {
    $array = unserialize($tmp_data);
}
//print_r($array);
# 장바구니 데이터 검사 --------------------------------------------------------------------------
if(is_array($array) && is_array($array['it_id']) && is_array($array['ct_chk'])) {
    # 장바구니 전송됨
} else {
    # 장바구니 전송안됨
    echo "<h2>견적서를 출력할 제품이 없습니다.</h2>";
    include_once(G5_PATH.'/tail.sub.php');
    exit;
}

# 장바구니 no 가공 ---------------------------------------------------------------------------
/* 장바구니 상품번호와 체크여부(ct_chk)를 비교하여 견적할 제품번호만 남긴다. */
if (isset($DATA['cart']) && is_array($DATA['cart'])) {
    foreach($array['it_id'] as $key=>$val) {
    if(isset($array['ct_chk'][$key]) && $array['ct_chk'][$key]=="1") {
        $TMP['it'][] = $val;
    } else {
        continue;
    }
}
}

//print_r($TMP['it']);
if(is_array($TMP['it']) && is_array($TMP['it'])) {
    # 견적할 제품이 있음
} else {
    # 견적할 제품이 없음
    echo "<h2>견적서를 출력할 제품이 없습니다.</h2>";
    include_once(G5_PATH.'/tail.sub.php');
    exit;
}

# 장바구니 DB 가져오기 ---------------------------------------------------------------------------
$s_cart_id = get_session('ss_cart_id');
// $s_cart_id 로 현재 장바구니 자료 쿼리
$qry = " SELECT * FROM {$g5['g5_shop_cart_table']} WHERE od_id = '$s_cart_id' AND it_id IN ( ". implode(",", $TMP['it']) ." ) ORDER BY it_id ";
$res = sql_query($qry);
if(sql_num_rows($res)>0) {
    $idx = 0;
    while($row = sql_fetch_array($res)) {
        $DATA['cart'][$idx] = $row;
        # 옵션DB 가져오기
        $qry2 = " SELECT * FROM {$g5['g5_shop_item_option_table']} WHERE it_id = '{$row['it_id']}' AND io_id='{$row['io_id']}' AND io_type='0' ";
        $res2 = sql_query($qry2);
        if(sql_num_rows($res2)>0) {
            $DATA['opt'][$idx] = sql_fetch_array($res2);
        }
        $idx++;
    }
}
//print_r($DATA);

# 출력화면 그리기      ---------------------------------------------------------------------------
?>

<style type="text/css">
.print-wrap { width:600px; box-sizing:border-box; margin:5px auto; }
/* 표제부 */
.table-info { border-collapse:collapse; width:100%; }
.table-info * { font-size: 12px;}
.table-info td { border:1px solid #09a4b6; height:20px; }
.table-info .td-row-6 { background:#06899f; color:#ffffff; text-align:center; }
.table-info .td-head { background:#67b9c2; text-align:center; color:#ffffff; }
/* 제품리스트 */
.table-list { border-collapse:collapse; width:100%; margin:20px 0 0 0; }
.table-list * { font-size:12px;}
.table-list td { border:1px solid #09a4b6; height:20px; }
.table-list .td-head { background:#67b9c2; text-align:center; color:#ffffff; }
.table-list .td-body { font-size:11px !important; }
/* 공통 */
.input-1 { width:174px; }
.td-right { text-align:right; }
.td-center { text-align:center; }
/* 기타 */
.div-help { margin:10px 0; color:#2861c8; }
.print-button { width:600px; margin:10px auto; text-align:right; }
.print-button button { padding:4px 10px; background:#2548a5; color:#ffffff; border:0; }
@media print {
    .print-button { display:none; }
}
</style>
<div class="print-wrap">
    <table class="table-info">
    <col width="20">
    <col width="60">
    <col width="180">
    <col width="20">
    <col width="60">
    <col width="">
    <col width="60">
    <col width="">
    <tr>
        <td class="td-row-6" rowspan="6">의<br>뢰<br>자</td>
        <td class="td-head">견적일</td>
        <td class="td-body"><input type="text" class="input-1" value="<?=date("Y-m-d")?>"></td>
        <td class="td-row-6" rowspan="6">공<br>급<br>자</td>
        <td class="td-head">등록번호</td>
        <td class="td-body" colspan="3"><?=$default['de_admin_company_saupja_no']?></td>
    </tr>
    <tr>
        <td class="td-head">업체명</td>
        <td class="td-body"><input type="text" class="input-1"></td>
        <td class="td-head">회사명</td>
        <td class="td-body"><?=$default['de_admin_company_name']?></td>
        <td class="td-head">성명</td>
        <td class="td-body"><?=$default['de_admin_company_owner']?></td>
    </tr>
    <tr>
        <td class="td-head">담당자</td>
        <td class="td-body"><input type="text" class="input-1"></td>
        <td class="td-head">주소</td>
        <td class="td-body" colspan="3"><?=$default['de_admin_company_addr']?></td>
    </tr>
    <tr>
        <td class="td-head">전화</td>
        <td class="td-body"><input type="text" class="input-1"></td>
        <td class="td-head">업태</td>
        <td class="td-body"><?=$default['de_biztype_a']?></td>
        <td class="td-head">종목</td>
        <td class="td-body"><?=$default['de_biztype_b']?></td>
    </tr>
    <tr>
        <td class="td-head">팩스</td>
        <td class="td-body"><input type="text" class="input-1"></td>
        <td class="td-head">전화</td>
        <td class="td-body"><?=$default['de_admin_company_tel']?></td>
        <td class="td-head">팩스</td>
        <td class="td-body"><?=$default['de_admin_company_fax']?></td>
    </tr>
    <tr>
        <td class="td-head">E-mail</td>
        <td class="td-body"><input type="text" class="input-1"></td>
        <td class="td-head">담당자</td>
        <td class="td-body" colspan="3"></td>
    </tr>
    </table>
    <table class="table-list">
    <col width="80">
    <col width="80">
    <col width="80">
    <col width="">
    <col width="60">
    <col width="40">
    <col width="70">
    <tr>
        <td class="td-head">제품번호</td>
        <td class="td-head">제품명</td>
        <td class="td-head">형식번호</td>
        <td class="td-head">제품사양</td>
        <td class="td-head">단가</td>
        <td class="td-head">수량</td>
        <td class="td-head">가격</td>
    </tr>
    <?
    $total_price = 0;
    $this_price = 0;
                                   
    foreach($DATA['cart'] as $key=>$val) {
        $this_price = ($val['ct_price']+$val['io_price']) * $val['ct_qty'];
        $total_price += $this_price;
        
    ?>
    <tr>
        <td class="td-body"><?php $val['io_id']?></td>
        <td class="td-body"><?php $val['it_name']?></td>
        <td class="td-body"><?php $DATA['opt'][$key]['io_misc01']?></td>
        <td class="td-body"><?php $DATA['opt'][$key]['io_misc02']?></td>
        <td class="td-body td-right"><?=number_format($val['ct_price']+$val['io_price'])?></td>
        <td class="td-body td-center"><?=number_format($val['ct_qty'])?></td>
        <td class="td-body td-right"><?=number_format($this_price)?></td>
    </tr>
    
    
    <?
    } # end foreach;
    ?>
    <tr>
        <td class="td-body td-center" colspan="6"><strong>합계금액</strong></td>
        <td class="td-body td-right"><strong><?=number_format($total_price)?></strong></td>
    </tr>

    </table>
    <div class="div-help">
        상기 견적내용은 환율에 따라 가격이 변동될 수 있으니 참고용으로만 활용하시기 바랍니다.<br>
        (견적내용 및 입력사항은 서버로 전송되지 않습니다)<br>
        자세한 견적상담을 원하시면 <?=$default['de_admin_company_tel']?>로 전화바랍니다.
    </div>
</div>
<div class="print-button">
    <button type="button" onClick="print();">인쇄</button>
    <button type="button" onClick="window.close();">닫기</button>
</div>
<?
include_once(G5_PATH.'/tail.sub.php');
?>

5.4.4.x 에서 해봤는데 오류는 없었어요. 

$TMP = array();

$TMP['it'] = array();
$DATA = array();
$DATA['cart'] = array();

미리 변수 초기화를 해둔 코드
이 코드로도 오류가 난다면 

lib/common.lib.php 업데이트를 해당 부분만이라도 해보시고요
(sql_ 로 시작하는 함수)

$array 변수는 (session: cartprint)

이런 식으로 되어야 정상적으로 출력될 것 같습니다.


$array = array(
    'it_id' => array('1490690285','1490690301'),
    'ct_chk' => array('1','1')
);

 

 


<?php
include_once('./_common.php');
include_once(G5_PATH.'/head.sub.php');
//print_r($_GET);
//print_r($_POST);
# 세션데이터에서 장바구니 전송값을 가져온다. ---------------------------------------------------
# [cart.php -> ajax.cartprint.php 세션저장한 값]
$tmp_data = get_session("cartprint");
if($tmp_data) {
    $array = unserialize($tmp_data);
}

//print_r($array);
# 장바구니 데이터 검사 --------------------------------------------------------------------------
if(is_array($array) && is_array($array['it_id']) && is_array($array['ct_chk'])) {
    # 장바구니 전송됨
} else {
    # 장바구니 전송안됨
    echo "<h2>견적서를 출력할 제품이 없습니다.</h2>";
    include_once(G5_PATH.'/tail.sub.php');
    exit;
}
# 장바구니 no 가공 ---------------------------------------------------------------------------
/* 장바구니 상품번호와 체크여부(ct_chk)를 비교하여 견적할 제품번호만 남긴다. */
$TMP = array();
$TMP['it'] = array();
$DATA = array();
$DATA['cart'] = array();
if (isset($array['it_id']) && is_array($array['it_id'])) {
foreach($array['it_id'] as $key=>$val) {
    if(isset($array['ct_chk'][$key]) && $array['ct_chk'][$key]=="1") {
        $TMP['it'][] = $val;
    } else {
        continue;
    }
}
}
//print_r($TMP['it']);
if(is_array($TMP['it']) && is_array($TMP['it'])) {
    # 견적할 제품이 있음
} else {
    # 견적할 제품이 없음
    echo "<h2>견적서를 출력할 제품이 없습니다.</h2>";
    include_once(G5_PATH.'/tail.sub.php');
    exit;
}
# 장바구니 DB 가져오기 ---------------------------------------------------------------------------
$s_cart_id = get_session('ss_cart_id');
// $s_cart_id 로 현재 장바구니 자료 쿼리
$qry = " SELECT * FROM {$g5['g5_shop_cart_table']} WHERE od_id = '$s_cart_id' AND it_id IN ( ". implode(",", $TMP['it']) ." ) ORDER BY it_id ";
$res = sql_query($qry) or die(sql_error_info());
if(sql_num_rows($res)>0) {
    $idx = 0;
    while($row = sql_fetch_array($res)) {
        $DATA['cart'][$idx] = $row;
        # 옵션DB 가져오기
        $qry2 = " SELECT * FROM {$g5['g5_shop_item_option_table']} WHERE it_id = '{$row['it_id']}' AND io_id='{$row['io_id']}' AND io_type='0' ";
        $res2 = sql_query($qry2);
        if(sql_num_rows($res2)>0) {
            $DATA['opt'][$idx] = sql_fetch_array($res2);
        }
        $idx++;
    }
}
//print_r($DATA);
# 출력화면 그리기      ---------------------------------------------------------------------------
?>
<style type="text/css">
.print-wrap { width:600px; box-sizing:border-box; margin:5px auto; }
/* 표제부 */
.table-info { border-collapse:collapse; width:100%; }
.table-info * { font-size: 12px;}
.table-info td { border:1px solid #09a4b6; height:20px; }
.table-info .td-row-6 { background:#06899f; color:#ffffff; text-align:center; }
.table-info .td-head { background:#67b9c2; text-align:center; color:#ffffff; }
/* 제품리스트 */
.table-list { border-collapse:collapse; width:100%; margin:20px 0 0 0; }
.table-list * { font-size:12px;}
.table-list td { border:1px solid #09a4b6; height:20px; }
.table-list .td-head { background:#67b9c2; text-align:center; color:#ffffff; }
.table-list .td-body { font-size:11px !important; }
/* 공통 */
.input-1 { width:174px; }
.td-right { text-align:right; }
.td-center { text-align:center; }
/* 기타 */
.div-help { margin:10px 0; color:#2861c8; }
.print-button { width:600px; margin:10px auto; text-align:right; }
.print-button button { padding:4px 10px; background:#2548a5; color:#ffffff; border:0; }
@media print {
    .print-button { display:none; }
}
</style>
<div class="print-wrap">
    <table class="table-info">
    <col width="20">
    <col width="60">
    <col width="180">
    <col width="20">
    <col width="60">
    <col width="">
    <col width="60">
    <col width="">
    <tr>
        <td class="td-row-6" rowspan="6">의<br>뢰<br>자</td>
        <td class="td-head">견적일</td>
        <td class="td-body"><input type="text" class="input-1" value="<?php echo date("Y-m-d")?>"></td>
        <td class="td-row-6" rowspan="6">공<br>급<br>자</td>
        <td class="td-head">등록번호</td>
        <td class="td-body" colspan="3"><?php echo $default['de_admin_company_saupja_no']?></td>
    </tr>
    <tr>
        <td class="td-head">업체명</td>
        <td class="td-body"><input type="text" class="input-1"></td>
        <td class="td-head">회사명</td>
        <td class="td-body"><?php echo $default['de_admin_company_name']?></td>
        <td class="td-head">성명</td>
        <td class="td-body"><?php echo $default['de_admin_company_owner']?></td>
    </tr>
    <tr>
        <td class="td-head">담당자</td>
        <td class="td-body"><input type="text" class="input-1"></td>
        <td class="td-head">주소</td>
        <td class="td-body" colspan="3"><?php echo $default['de_admin_company_addr']?></td>
    </tr>
    <tr>
        <td class="td-head">전화</td>
        <td class="td-body"><input type="text" class="input-1"></td>
        <td class="td-head">업태</td>
        <td class="td-body"><?php echo $default['de_biztype_a']?></td>
        <td class="td-head">종목</td>
        <td class="td-body"><?php echo $default['de_biztype_b']?></td>
    </tr>
    <tr>
        <td class="td-head">팩스</td>
        <td class="td-body"><input type="text" class="input-1"></td>
        <td class="td-head">전화</td>
        <td class="td-body"><?php echo $default['de_admin_company_tel']?></td>
        <td class="td-head">팩스</td>
        <td class="td-body"><?php echo $default['de_admin_company_fax']?></td>
    </tr>
    <tr>
        <td class="td-head">E-mail</td>
        <td class="td-body"><input type="text" class="input-1"></td>
        <td class="td-head">담당자</td>
        <td class="td-body" colspan="3"></td>
    </tr>
    </table>
    <table class="table-list">
    <col width="80">
    <col width="80">
    <col width="80">
    <col width="">
    <col width="60">
    <col width="40">
    <col width="70">
    <tr>
        <td class="td-head">제품번호</td>
        <td class="td-head">제품명</td>
        <td class="td-head">형식번호</td>
        <td class="td-head">제품사양</td>
        <td class="td-head">단가</td>
        <td class="td-head">수량</td>
        <td class="td-head">가격</td>
    </tr>
    <?php
    $total_price = 0;
    $this_price = 0;               
    if (isset($DATA['cart']) && is_array($DATA['cart'])) {
    foreach($DATA['cart'] as $key=>$val) {
        $this_price = ($val['ct_price']+$val['io_price']) * $val['ct_qty'];
        $total_price += $this_price;
        
    ?>
    <tr>
        <td class="td-body"><?php echo $val['io_id']?></td>
        <td class="td-body"><?php echo $val['it_name']?></td>
        <td class="td-body"><?php echo $DATA['opt'][$key]['io_misc01']?></td>
        <td class="td-body"><?php echo $DATA['opt'][$key]['io_misc02']?></td>
        <td class="td-body td-right"><?php echo number_format($val['ct_price']+$val['io_price'])?></td>
        <td class="td-body td-center"><?php echo number_format($val['ct_qty'])?></td>
        <td class="td-body td-right"><?php echo number_format($this_price)?></td>
    </tr>
    <?php
    } # end foreach;
    } # end of if
    ?>
    <tr>
        <td class="td-body td-center" colspan="6"><strong>합계금액</strong></td>
        <td class="td-body td-right"><strong><?php echo number_format($total_price)?></strong></td>
    </tr>
    </table>
    <div class="div-help">
        상기 견적내용은 환율에 따라 가격이 변동될 수 있으니 참고용으로만 활용하시기 바랍니다.<br>
        (견적내용 및 입력사항은 서버로 전송되지 않습니다)<br>
        자세한 견적상담을 원하시면 <?php echo $default['de_admin_company_tel']?>로 전화바랍니다.
    </div>
</div>
<div class="print-button">
    <button type="button" onClick="print();">인쇄</button>
    <button type="button" onClick="window.close();">닫기</button>
</div>
<?php
include_once(G5_PATH.'/tail.sub.php');
답변을 작성하시기 전에 로그인 해주세요.
전체 293
QA 내용 검색

회원로그인

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