채택완료

내 총 구매금액 계산하기

2018-10-10 (수) 17:46:55 4,240
Copy
<?php
function array_msort($array, $cols){ // 배열 재정열 함수
    $colarr = array();
    foreach ($cols as $col => $order) {
        $colarr[$col] = array();
        foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
    }
    $eval = 'array_multisort(';
    foreach ($cols as $col => $order) {
        $eval .= '$colarr[\''.$col.'\'],'.$order.',';
    }
    $eval = substr($eval,0,-1).');';
    eval($eval);
    $ret = array();
    foreach ($colarr as $col => $arr) {
        foreach ($arr as $k => $v) {
            $k = substr($k,1);
            if (!isset($ret[$k])) $ret[$k] = $array[$k];
            $ret[$k][$col] = $array[$k][$col];
        }
    }
    return array_values($ret);
}
?>
<p>단골 고객<span>(비회원은 포함되지 않습니다.)</span></p>
<table>
    <tr>
        <th>순위</th>
        <th>회원 ID</th>
        <th>이름</th>
        <th>누적 주문</th>
        <th>총 구매금액</th>
    </tr>
    <?php
    $ranking = array();
    $list = 0;
    $sql = "select a.mb_id,a.od_cart_price,b.mb_name from {$g5['g5_shop_order_table']} a inner join {$g5['member_table']} b on a.mb_id = b.mb_id where a.od_status != '취소' and a.mb_id != ''"; // 주문상태 취소를 제외, 비회원 제외하고 불러옵니다.
    $rs = sql_query($sql);
    while($row = sql_fetch_array($rs)){
        $search_key = array_search($row['mb_id'], array_column($ranking, 'mb_id'));
        if($search_key > -1){
            $ranking[$search_key]['price'] += $row['od_cart_price'];
            $ranking[$search_key]['count'] += 1;
        } else {
            $ranking[$list]['mb_id'] = $row['mb_id'];
            $ranking[$list]['mb_name'] = $row['mb_name'];
            $ranking[$list]['price'] = $row['od_cart_price'];
            $ranking[$list]['count'] = 1;
            $list++;
        }
    }
    $rank = array_msort($ranking, array('price'=>SORT_DESC)); // 배열 재정열
    if(count($rank) > 7) $count = 7; // 출력 갯수
    else $count = count($rank);
    for($i=0; $i<$count; $i++){
    ?>
    <tr>
        <td><?php echo $i + 1;?></td>
        <td><?=$rank[$i]['mb_id']?></td>
        <td><?=$rank[$i]['mb_name']?></td>
        <td><?=number_format($rank[$i]['count'])?><span>건</span></td>
        <td><?=number_format($rank[$i]['price'])?><span>원</span></td>
    </tr>
    <?php } if($i == 0){?>
    <tr>
        <td colspan="50">아직 단골 고객이 없습니다.</td>
    </tr>
    <?php }?>
</table>

이 소스에서 내 총 구매금액만 구하고 싶은데요

필요한 소스만 빼서 간략하게 해주실 분 있나요? ㅠㅠ

|

답변 2개 / 댓글 5개

채택된 답변
+20 포인트
Copy
$sql = "select sum(a.od_cart_price) od_cart_price from {$g5['g5_shop_order_table']} a inner join {$g5['member_table']} b on a.mb_id = b.mb_id where a.od_status != '취소' and a.mb_id == '{$member['mb_id']}'"; // 주문상태 취소를 제외,  
$row = sql_query($sql);

echo $row['od_cart_price']; // 내 총금액

딱히 설명해드릴만큼 어려운 부분은 없네요.

답변에 대한 댓글 4개

엌 감사합니다 ㅠㅠ 저는 일자무식이라 약간의 변형 밖에 못해서요 감사합니다 정말
<?php
$sql = "select sum(a.od_cart_price) od_cart_price from {$g5['g5_shop_order_table']} a inner join {$g5['member_table']} b on a.mb_id = b.mb_id where a.od_status != '취소' and a.mb_id == '{$member['mb_id']}'"; // 주문상태 취소를 제외,
$row = sql_query($sql);
?>
<?php
echo $row['od_cart_price']; // 내 총금액
?>
이렇게 했는데 아무것도 안나올땐 어떡하죠? ㅠㅠ
$sql = "select sum(a.od_cart_price) od_cart_price from {$g5['g5_shop_order_table']} a inner join {$g5['member_table']} b on a.mb_id = b.mb_id where a.od_status != '취소' and a.mb_id = '{$member['mb_id']}'"; // 주문상태 취소를 제외,

쿼리문을 이렇게 다시 바꿔보세요. 조건문 부호가 잘못되어 있엇네요.
Fatal error: Uncaught Error: Cannot use object of type mysqli_result as array in ii.php:10 Stack trace: #0 {main} thrown in ii.php on line 10

이렇게 에러코드가 나와요

<?php
include_once('./_common.php');
include_once(G5_THEME_PATH.'/head.php');
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
<?php
$sql = "select sum(a.od_cart_price) od_cart_price from {$g5['g5_shop_order_table']} a inner join {$g5['member_table']} b on a.mb_id = b.mb_id where a.od_status != '취소' and a.mb_id = '{$member['mb_id']}'"; // 주문상태 취소를 제외,
$row = sql_query($sql);
echo $row['od_cart_price']; // 내 총금액
?>
<?php
include_once(G5_THEME_PATH.'/tail.php');
?>

$row = sql_query($sql); 

를

$row = sql_fetch($sql); 

로 변경해보세요.

테스트 안해보고 알려드렸더니 에러가 좀 있네요.

답변에 대한 댓글 1개

와 됐어요~! 너무너무너무 감사합니다 복 받으세요 ㅠㅠ

답변을 작성하려면 로그인이 필요합니다.