db 배열값에 넣고 풀기
본문
chart.js를 사용하고잇고
밑에 날짜 값은 가지고 왓습니다
<?php $sub_menu = '400010'; include_once('./_common.php'); $max_limit = 7; // 몇행 출력할 것인지? $g5['title'] = ' 쇼핑몰현황'; include_once(G5_MNG_PATH . '/admin.head.php'); ?> <?php unset($save); unset($tot); $cnt = 0; $margin = $member["mb_5"]; $bx_pay = intval($pay * $margin / 100); $date = ''; $sql = "SELECT SUM(ct_price) as total_pay, COUNT(*) as cnt FROM `g5_shop_cart` cart JOIN g5_shop_item itm ON cart.it_id = itm.it_id WHERE itm.it_2_subj ='{$member["mb_id"]}' AND at_status=0 AND SUBSTRING(ct_time,1,10) = '$date'"; $row = sql_fetch($sql); ?> <div class="chart-container" style="position: relative; height:100; width:100"> <canvas id="myChart"></canvas> </div> <?php $arr_total = array(); $x_val = array(); for ($i = 6; $i >= 0; $i--) { $date= date('Y-m-d', strtotime('-' . $i . ' days', G5_SERVER_TIME)); $x_val[] = $date; $arr_total[] = $date; print_r($arr_total[$i]); } for ($i = 0; $i < count($x_val); $i++) { // var_dump($arr_total[$i]) ; } ?> <script src="https://cdn.jsdelivr.net/npm/chart.js@3.7.0/dist/chart.min.js"></script> <script> const labels = [ <?php $term = 7; for($i = ($term - 1); $i >= 0; $i--) { $date = date("Y-m-d", strtotime('-' . $i . ' days', G5_SERVER_TIME)); $day = substr($date, 5, 5) . ' (' . get_yoil($date) . ')'; ?> '<?= $day ?>', <?php } ?> ]; const data = { labels: labels, datasets: [{ label: '마이 머니', backgroundColor: 'rgb(255, 99, 132)', borderColor: 'rgb(255, 99, 132)', data: [0, <?= $row['total_pay'] ?>, 5, 2, 20,], }] }; const config = { type: 'line', data: data, options: { responsive: true, animation: { animateScale: true, animateRotate: true } } }; </script> <script> const myChart = new Chart( document.getElementById('myChart'), config ); </script> <?php include_once(G5_MNG_PATH . '/admin.tail.php'); ?>
sql의 값을 date에 각각 넣고 배열에 담아서
datasets: [{ label: '마이 머니', backgroundColor: 'rgb(255, 99, 132)', borderColor: 'rgb(255, 99, 132)', data: [0, <?= $row['total_pay'] ?>, 5, 2, 20,], }]
data의 값에 sql을 각각 집어넣어 배열로 묶어 for문으로 $row['total_pay']값을 풀어서
각 날짜마다 그래프를 나타내주려고 하는데
어떻게 해야되나요;;
제 어리석은 생각으로는
<?php $arr_total = array(); for ($i = 6; $i >= 0; $i--) { $date= date('Y-m-d', strtotime('-' . $i . ' days', G5_SERVER_TIME)); array_push($arr_total,$date); } ?> <?php for($k=0; $k < $arr_total.length; $k++) { $sql = "SELECT SUM(ct_price) as total_pay, COUNT(*) as cnt FROM `g5_shop_cart` cart JOIN g5_shop_item itm ON cart.it_id = itm.it_id WHERE itm.it_2_subj ='{$member["mb_id"]}' AND at_status=0 AND SUBSTRING(ct_time,1,10) = '$arr_total[$k]'"; $row = sql_fetch($sql); } var_dump($row); ?>
갑자기
어질어질 하네요 ...
!-->!-->
답변 3
$sql = "SELECT SUM(ct_price) as total_pay,
COUNT(*) as cnt FROM `g5_shop_cart` cart JOIN
g5_shop_item itm ON cart.it_id = itm.it_id
WHERE itm.it_2_subj ='{$member["mb_id"]}' AND at_status=0
and ct_time > 'date('Y-m-d', strtotime('-6 days', G5_SERVER_TIME))'
group by SUBSTRING(ct_time,1,10)
테스트 해보진 않았지만 이렇게 group 이로 묶으면 될것 같습니다.
!-->
g5_shop_cart를 사용하는 것을 보니
매출 관련 내용인 듯한데
매출이 없는 날짜에는 0이 나오게 하려면
(필요한) 모든 날짜를 db 테이블에 다 넣고
이 테이블과 매출 테이블을 left 하시면 됩니다.
자문자답입니다
<?php
$sub_menu = '400010';
include_once('./_common.php');
$max_limit = 7; // 몇행 출력할 것인지?
$g5['title'] = ' 쇼핑몰현황';
include_once(G5_MNG_PATH . '/admin.head.php'); ?>
<?php
$mb_id = $member["mb_id"];
function get_total_pay($date)
{
global $g5;
global $mb_id;
$sql = "SELECT SUM(ct_price) as total_pay,
COUNT(*) as cnt FROM {$g5['g5_shop_cart_table']} cart JOIN
{$g5['g5_shop_item_table']} itm ON cart.it_id = itm.it_id
WHERE itm.it_2_subj ='{$mb_id}' AND at_status=0 AND ct_status = '완료'
AND SUBSTRING(ct_time,1,10) = '$date'";
$row = sql_fetch($sql);
$total_pay = (int)$row['total_pay'];
return $total_pay;
}
$arr_total = array();
for ($i = 6; $i >= 0; $i--) {
$date = date('Y-m-d', strtotime('-' . $i . ' days', G5_SERVER_TIME));
$arr_total[] = get_total_pay($date);
}
?>
<div class="chart-container" style="position: relative; height:100; width:100">
<canvas id="myChart"></canvas>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.7.0/dist/chart.min.js"></script>
<script>
const labels = [
<?php
$term = 7;
for($i = ($term - 1); $i >= 0; $i--) {
$date = date("Y-m-d", strtotime('-' . $i . ' days', G5_SERVER_TIME));
$day = substr($date, 5, 5) . ' (' . get_yoil($date) . ')';
?>
'<?= $day ?>',
<?php } ?>
];
const data = {
labels: labels,
datasets: [{
label: '마이 머니',
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: [<?php
for ($k = 0; $k < count($arr_total); $k++) {
echo $arr_total[$k] . ',';
}
?>],
}]
};
const config = {
type: 'line',
data: data,
options: {
responsive: true,
animation: {
animateScale: true,
animateRotate: true
}
}
};
</script>
<script>
const myChart = new Chart(
document.getElementById('myChart'),
config
);
</script>
<?php
include_once(G5_MNG_PATH . '/admin.tail.php');
?>
답변을 작성하시기 전에 로그인 해주세요.