테스트 사이트 - 개발 중인 베타 버전입니다

내 총 구매금액 계산하기 채택완료

블랑숑 7년 전 조회 3,920

</p>

<p><?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></p>

<p>

 

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

 

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

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

답변 2개

채택된 답변
+20 포인트
잉끼s
7년 전

</p>

<p>$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);</p>

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

<p>

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

로그인 후 평가할 수 있습니다

답변에 대한 댓글 4개

블랑숑
7년 전
엌 감사합니다 ㅠㅠ 저는 일자무식이라 약간의 변형 밖에 못해서요 감사합니다 정말
블랑숑
7년 전
<?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']; // 내 총금액
?>
이렇게 했는데 아무것도 안나올땐 어떡하죠? ㅠㅠ
잉기
7년 전
$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']}'"; // 주문상태 취소를 제외,

쿼리문을 이렇게 다시 바꿔보세요. 조건문 부호가 잘못되어 있엇네요.
블랑숑
7년 전
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');
?>

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

잉끼s
7년 전

$row = sql_query($sql); 

$row = sql_fetch($sql); 

로 변경해보세요.

 

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

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

블랑숑
7년 전
와 됐어요~! 너무너무너무 감사합니다 복 받으세요 ㅠㅠ

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

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

로그인