반복되는 구문 간단하게 하는 방법 질문입니다.
본문
안녕하세요!
배열(?): 도움을 구합니다.
아래와 같은 내용인데 동일한 내용으로 20개 가까이 일부 항목만 바뀌어 배열이 됩니다.
이걸 간단하게 처리하고 싶은데 프로그램 문외한이라 고수님들께 도움을 청합니다.
간단하게 할 수 있는 방법 좀 부탁드립니다.
<?
$sql1 = " select count(*) as cnt from {$write_table} where ca_name = 'AAA' and mb_id = '".$member['mb_id']."'";
$row1 = sql_fetch($sql1);
$com_count1 = $row1['cnt'];
?>
<img src="<?php echo G5_THEME_URL; ?>/img/icon_list.png"> AAA : <?=$com_count1?> 개
<br>
<br>
<?
$sql2 = " select count(*) as cnt from {$write_table} where ca_name = 'BBB' and mb_id = '".$member['mb_id']."'";
$row2 = sql_fetch($sql2);
$com_count2 = $row2['cnt'];
?>
<img src="<?php echo G5_THEME_URL; ?>/img/icon_list.png"> BBB : <?=$com_count2?> 개
<br>
<br>
<?
$sql3 = " select count(*) as cnt from {$write_table} where ca_name = '345345' and mb_id = '".$member['mb_id']."'";
$row3 = sql_fetch($sql3);
$com_count3 = $row3['cnt'];
?>
<img src="<?php echo G5_THEME_URL; ?>/img/icon_list.png"> 345345 : <?=$com_count3?> 개
<br>
<br>
<?
$sql4 = " select count(*) as cnt from {$write_table} where ca_name = '가가가' and mb_id = '".$member['mb_id']."'";
$row4 = sql_fetch($sql4);
$com_count4 = $row4['cnt'];
?>
<img src="<?php echo G5_THEME_URL; ?>/img/icon_list.png"> 가가가 : <?=$com_count4?> 개
<br>
<br>
<?
$sql5 = " select count(*) as cnt from {$write_table} where ca_name = '222222' and mb_id = '".$member['mb_id']."'";
$row5 = sql_fetch($sql5);
$com_count5 = $row5['cnt'];
?>
<img src="<?php echo G5_THEME_URL; ?>/img/icon_list.png"> 222222 : <?=$com_count5?> 개
<br>
<br>
<?
$sql6 = " select count(*) as cnt from {$write_table} where ca_name = 'K57O' and mb_id = '".$member['mb_id']."'";
$row6 = sql_fetch($sql6);
$com_count6 = $row6['cnt'];
?>
<img src="<?php echo G5_THEME_URL; ?>/img/icon_list.png"> K57O : <?=$com_count6?> 개
<br>
<br>
<?
$sql7 = " select count(*) as cnt from {$write_table} where ca_name = 'SSD2' and mb_id = '".$member['mb_id']."'";
$row7 = sql_fetch($sql7);
$com_count7 = $row7['cnt'];
?>
<img src="<?php echo G5_THEME_URL; ?>/img/icon_list.png"> SSD2 : <?=$com_count7?> 개
답변 2
배열을 이용하여 반복문으로 처리하면 됩니다.
<?php
$texts = array(
"AAA",
"BBB",
"345345",
"가가가",
"222222",
"K570",
"SSD2",
);
foreach ($texts as $key => $value) {
$sql1 = " select count(*) as cnt from {$write_table} where ca_name = '{$value}' and mb_id = '".$member['mb_id']."'";
$row1 = sql_fetch($sql1);
$com_count1 = $row1['cnt'];
?>
<img src="<?php echo G5_THEME_URL; ?>/img/icon_list.png"> <?php echo $value; ?> : <?=$com_count1?> 개
<br>
<br>
<?php
} ?>
$arrCate = array(
'AAA', 'BBB', 'CCC'
);
$arrHtml = array();
foreach ($arrCate as $v) {
$query = "
SELECT COUNT(*) AS cnt
FROM {$write_table}
WHERE ca_name = '{$v}' AND mb_id = '{$member['mb_id']}';
";
$cnt = end(sql_fetch($query));
$arrHtml[] = "
<img src=\"" . G5_THEME_URL . "/img/icon_list.png\"> {$v} : {$cnt} 개
<br /><br />
";
}
echo implode('', $arrHtml);
답변을 작성하시기 전에 로그인 해주세요.