for 문 count 문제
본문
안녕하세요? 항상 도움 받아서 기능 구현을 하고 있습니다.
제가 DB 에서 상품 정보를 가져와서 table 로 보여주는데 count 에서 문제가 생겼습니다. 당연히 count($result) 값이 0 이상일텐데 0 으로 아무것도 안 나오네요. 제 코드 좀 봐주시겠어요?
항상 감사합니다.
<?php
include_once('./_common.php');
// 보관기간이 지난 상품 삭제
cart_item_clean();
// cart id 설정
set_cart_id($sw_direct);
if($sw_direct)
$tmp_cart_id = get_session('ss_cart_direct');
else
$tmp_cart_id = get_session('ss_cart_id');
// 브라우저에서 쿠키를 허용하지 않은 경우라고 볼 수 있음.
if (!$tmp_cart_id)
{
alert('더 이상 작업을 진행할 수 없습니다.\\n\\n브라우저의 쿠키 허용을 사용하지 않음으로 설정한것 같습니다.\\n\\n쿠키 허용을 사용함으로 설정하신 후 다시 시도해 주십시오.\\n\\n그래도 진행이 되지 않는다면 쇼핑몰 운영자에게 문의 바랍니다.');
}
$tmp_cart_id = preg_replace('/[^a-z0-9_\-]/i', '', $tmp_cart_id);
$sql = " select * from {$g5['g5_shop_cart_table']} where od_id = '$tmp_cart_id' ";
$result = sql_fetch($sql);
$image = get_it_image($result['it_id'], 80, 80);
?>
<div>
<?php
echo get_it_image($result['it_id'], 80, 80);
echo $result['it_id'];
echo get_text($result['it_name']);
echo number_format($result['ct_qty']);
?>
</div>
<div class="table-responsive order-item">
<table id="sod_list" class="div-table table bg-white bsk-tbl">
<tbody>
<tr class="<?php echo $head_class;?>">
<th scope="col"><span>이미지</span></th>
<th scope="col"><span>상품명</span></th>
<th scope="col"><span>총수량</span></th>
</tr>
<?php for($i=0; $i < count($result); $i++) { ?>
<tr>
<td class="text-center">
<div class="item-img">
<?php echo get_it_image($result[$i]['it_id'], 70, 70); ?>
</div>
</td>
<td class="text-center"><?php echo get_text($result[$i]['it_name']); ?></td>
<td class="text-center"><?php echo number_format($result[$i]['ct_qty']); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
답변 4
$result = sql_fetch($sql);
대신에
$result = sql_query($sql);
<?php for($i=0; $i < count($result); $i++) { ?>
가 아니고
for($i = 0; $row = sql_fetch_array($result); $i++) {
또는
while( $row = sql_fetch_array($result))
※ https://marscompany.tistory.com/24
$sql = " select * from {$g5['g5_shop_cart_table']} where od_id = '$tmp_cart_id' ";
$result = sql_query($sql);
<?php for($i = 0; $row = sql_fetch_array($result); $i++) { ?>
<tr>
<td class="text-center">
<div class="item-img">
<?php echo get_it_image($row['it_id'], 70, 70); ?>
</div>
</td>
<td class="text-center"><?php echo get_text($row['it_name']); ?></td>
<td class="text-center"><?php echo number_format($row['ct_qty']); ?></td>
</tr>
<?php } ?>
$sql 값을 echo 화면에 출력시켜서 쿼리분석기로 돌려보세요~
for($i=0;$row=sql_fetch_array($result);$i++)
{
}
으로 하면 출력가능하실것 같으세요