더하기 너무 어렵네요

더하기 너무 어렵네요

QA

더하기 너무 어렵네요

본문

현재 아래와 같이 뽑았는데요..

 
년-월 그래프 월별입금 월별환불 월별실적 비율(%)
총계 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로 변경하세요

폰으로 작성하니 변수가 안보여서
댓글로 추가합니다

$arr2 =array();

for ($i=0; $row=sql_fetch_array($result); $i++) {
    $arr[$row['vs_month']] = $row['total'];
$arr2[$row['vs_month']] = $row['refund'];
    if ($row['total'] > $max) $max = $row['total'];

    $sum_count += $row['total'];


    $sum_refund += $row['refund'];

}


...

하단에서


  <td class="td_numbig"><?php echo number_format($row[refund]) ?></td>

영역을

  <td class="td_numbig"><?php echo number_format($arr2[$key])?></td>
로 해주셔야 할것 같네요

<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++) {

}

 

이거 한번더 쓰셔서 처리하시는게 좋을것같은데요

내가 한게아니에요,,https://sir.kr/g5_plugin/199 이분이 그렇게 한거에요,,ㅋ

어쨌거나 이렇게 했는데 간단히 안되네요ㅡㅡ

$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'];

}

$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++) {
    $arr[$row['vs_month']] = $row['refund'];



    if ($row['refund'] > $max) $max = $row['refund'];


    $sum_refund += $row['refund'];

}

답변을 작성하시기 전에 로그인 해주세요.
전체 123,531 | RSS
QA 내용 검색

회원로그인

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