从当前,最后,上个月选择数据并在图表上回显

时间:2014-02-03 08:10:57

标签: php mysql google-visualization

您好我需要显示3个月(单独)扣除的总金额并在列表中显示。

(我已经包含了session_start和$ user_id = $ _SESSION ['user_id'])

而且我认为我的$ query2和$ query3是错误的,我想从上个月选择总和而不是结合上个月和&这个月。

因此,$query1 =当月的SUM,$query2 =上一个月的SUM,$query3 =过去两个月的SUM。

$query1 = "SELECT SUM(debit) AS current_debit FROM account WHERE user_id='$user_id' AND MONTH(CURDATE())= MONTH(date)";
$result1 = mysqli_query($link, $query1) or die(mysqli_error($link));
$row = mysqli_fetch_array($result1);
$currentMonth_debit = $row ['current_debit'];

$query2 = "SELECT SUM(debit) AS last_debit FROM account WHERE user_id='$user_id' AND date BETWEEN CURRENT_DATE - INTERVAL 1 MONTH AND CURRENT_DATE";
$result2 = mysqli_query($link, $query2) or die(mysqli_error($link));
$row2 = mysqli_fetch_array($result2);
$lastMonth_debit = $row2 ['last_debit'];

$query3 = "SELECT SUM(debit) AS prev_debit FROM account WHERE user_id='$user_id' AND date BETWEEN CURRENT_DATE - INTERVAL 2 MONTH AND CURRENT_DATE";
$result3 = mysqli_query($link, $query3) or die(mysqli_error($link));
$row3 = mysqli_fetch_array($result3);
$prevMonth_debit = $row3 ['prev_debit'];

之后,如何在Google图表上显示$ currentMonth_debit,$ lastMonth_debit,$ prevMonth_debit?我应该创建一个类似..

的数组
$pieData[] = array($row['$currentMonth_debit'], $row['lastMonth_debit'], $row['$prevMonth_debit']);

这是我的谷歌图表页面,

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Month', 'Money Out'],
      [<?php echo $pieData ?>],
    ]);

    var options = {
      title: 'Total Money Out',
      hAxis: {title: 'Month', titleTextStyle: {color: 'red'}}
    };

    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>

当我运行此代码时,图表不会出现。请帮忙! :(

1 个答案:

答案 0 :(得分:1)

首先,请为您自己和您的用户提供帮助,并在查询中使用参数绑定:

$query1 = "SELECT SUM(debit) AS current_debit FROM account WHERE user_id=? AND MONTH(CURDATE())= MONTH(date)";
$stmt = mysqli_prepare($link, $query1) or die(mysqli_error($link));
mysqli_stmt_bind_param($stmt, 's', $user_id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $currentDebit);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);

$query2 = "SELECT SUM(debit) AS last_debit FROM account WHERE user_id=? AND date BETWEEN CURRENT_DATE - INTERVAL 1 MONTH AND CURRENT_DATE";
$stmt = mysqli_prepare($link, $query1) or die(mysqli_error($link));
mysqli_stmt_bind_param($stmt, 's', $user_id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $lastDebit);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);

$query3 = "SELECT SUM(debit) AS prev_debit FROM account WHERE user_id=? AND date BETWEEN CURRENT_DATE - INTERVAL 2 MONTH AND CURRENT_DATE";
$stmt = mysqli_prepare($link, $query1) or die(mysqli_error($link));
mysqli_stmt_bind_param($stmt, 's', $user_id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $prevDebit);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);

为了使查询正确,您必须确定“当前月份”,“上个月”和“过去2个月”的具体含义。

将数据放入正确的格式很简单:

$pieData = array(
    array('Month', 'Money Out'),
    array('Current Month', $currentMonth),
    array('Last Month', $lastMonth),
    array('Previous 2 months', $prevMonth)
);

并在您的javascript中:

var data = google.visualization.arrayToDataTable(<?php echo json_encode($pieData, JSON_NUMERIC_CHECK); ?>);