더하기 너무 어렵네요
본문
현재 아래와 같이 뽑았는데요..
년-월 | 그래프 | 월별입금 | 월별환불 | 월별실적 | 비율(%) |
---|---|---|---|---|---|
총계 | 227,451 | -8,108 | 100% | ||
2019-04 | 26,327 | 0 | 11.6 | ||
2019-03 | 59,622 | 0 | 26.2 | ||
2019-02 | 71,399 | 0 | 31.4 | ||
2019-01 | 70,102 | 0 | 30.8 |
월별환불은 데이터를 뽑아오지 못하네요... 0으로 나와버리는 상황입니다.
wr_255값도 월별로 뽑아야 하거든요..
코드는 다음과 같습니다. 어떻게 고쳐야 월별환불 칸에 나올까요?
<?php
$sub_menu = "200830";
include_once('./_common.php');
auth_check($auth[$sub_menu], 'r');
$g5['title'] = '월별 실적';
include_once('./member_count.sub.php');
$colspan = 6;
$max = 0;
$sum_total = 0;
$sql = " select SUBSTRING(wr_datetime,1,7) as vs_month, sum(wr_8) as total, sum(wr_255) as refund
from g5_write_admin
where date(wr_datetime) between '{$fr_date}' and '{$to_date}'
group by vs_month
order by vs_month desc";
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++) {
$arr[$row['vs_month']] = $row['total'];
if ($row['total'] > $max) $max = $row['total'];
$sum_count += $row['total'];
$sum_refund += $row['refund'];
}
?>
<div class="tbl_head01 tbl_wrap">
<table>
<caption><?php echo $g5['title']; ?> 목록</caption>
<thead>
<tr>
<th scope="col">년-월</th>
<th scope="col">그래프</th>
<th scope="col">월별입금</th>
<th scope="col">월별환불</th>
<th scope="col">월별실적</th>
<th scope="col">비율(%)</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="2">총계</td>
<td><strong><?php echo number_format($sum_count) ?></strong></td>
<td><?php echo number_format($sum_refund) ?></td>
<td></td>
</td></td>
<td>100%</td>
</tr>
</tfoot>
<tbody>
<?php
$i = 0;
$k = 0;
$save_count = -1;
$tot_count = 0;
if (count($arr)) {
foreach ($arr as $key=>$value) {
$count = $value;
$rate = ($count / $sum_count * 100);
$s_rate = number_format($rate, 1);
$bg = 'bg'.($i%2);
?>
<tr class="<?php echo $bg; ?>">
<td class="td_category"><a href="./visit_date.php?fr_date=<?php echo $key ?>-01&to_date=<?php echo $key ?>-31"><?php echo $key ?></a></td>
<td>
<div class="visit_bar">
<span style="width:<?php echo $s_rate ?>%"></span>
</div>
</td>
<td class="td_numbig"><?php echo number_format($value) ?></td>
<td class="td_numbig"><?php echo number_format($row[refund]) ?></td>
<td class="td_numbig"></td>
<td class="td_num"><?php echo $s_rate ?></td>
</tr>
<?php
}
} else {
echo '<tr><td colspan="'.$colspan.'" class="empty_table">자료가 없습니다.</td></tr>';
}
?>
</tbody>
</table>
</div>
<?php
include_once('./admin.tail.php');
?>
답변 3
wr_255에 0이나 숫자가 아닌 값이 있나요?
데이타를 좀 정제하신후에 sum을 돌리시는게 나을거 같네요
한방쿼리로 만들더라도
sub_query로 추가하셔야 할듯 한데..
그래도
sum(if(wr_255 >= 0,wr_255,if(wr_255 < 0,wr_255,0))) as refund
로 대치해보세요
숫자가 아니면 0으로 변경하는겁니다
그리고,
추출한 re_fund를 변수상에 넣는 부분이 없네요
하단에 출력을 처리할때는 $row로 접근한것도 잘못이구요
다시말해
$arr에 total을 넣듯
$arr2를 하나 만드시고
for문 전에 $arr2= array();
를 선언하고
$arr과 같은 패턴으로 $row['refund']를 대입해서
하단에 $row영역에 $arr2로 변경하세요
<td class="td_numbig"><?php echo number_format($row[refund]) ?></td>
이부분 $row 이건 위에 for에 있고 아래의 foreach 에는 없네요.
foreach 를 쓸이유가 없는데 왜 꼭 저기에 넣으셨나요?
for문 아래에서 한번더 돌리면 간단하게 해결되요 ㅎㅎ
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++) {
}
이거 한번더 쓰셔서 처리하시는게 좋을것같은데요