如何在图表中显示每周的每周数据?

时间:2013-10-10 06:58:25

标签: php mysql graph

我有一张图表显示了一个人预订的展位总数,但我想显示一个人每天预订的展位数量。

以下是显示预订总额的代码:

   <?php
   $current_day = date("N");
    $days_to_sat = 6 - $current_day;
   $days_from_sun = $current_day - 0;
  $monday = date("d-m-y", strtotime("- {$days_from_sun} Days"));
    $friday = date("d-m-y", strtotime("+ {$days_to_sat} Days"));
     echo  $monday."&nbsp; To &nbsp;".$friday;



  $c=$_SESSION['user'];
   $sth =mysql_query("SELECT count(`booth_number`) AS 'booth',`sold_by` FROM `registration1` where week(`Date`) = week(curdate()) and where `sold_by`= '$c' ");

  $rows = array();
  $flag = true;
  $table = array();
  $table['cols'] = array(

  array('label' => 'sold_by', 'type' => 'string'),
  array('label' => 'booth', 'type' => 'number')

  );

 $rows = array();
  while($r = mysql_fetch_assoc($sth)) {
  $temp = array();
   $temp[] = array('v' => (string) $r['sold_by']); 

  $temp[] = array('v' => (int) $r['booth']); 
 $rows[] = array('c' => $temp);
     }

  $table['rows'] = $rows;
  $jsonTable = json_encode($table);

 ?>

<!--Load the Ajax API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">

// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

function drawChart() {

  // Create our data table out of JSON data loaded from server.
  var data = new google.visualization.DataTable(<?=$jsonTable?>);
 var options = {
     title:" Total Booth Booked ",
      width: 400, height: 200,

      minorTicks: 5
    };
  // Instantiate and draw our chart, passing in some options.
  // Do not forget to check your div ID
  var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
  chart.draw(data, options);
}
</script>

<!--this is the div that will hold the pie chart-->
<div id="chart_div"></div>

1 个答案:

答案 0 :(得分:0)

获取本周的所有日子:

$week = array();
$start = new DateTime;
$start->setISOdate($start->format('o'), $start->format('W'));
$days = new DatePeriod($start, new DateInterval('P1D'), 6);
foreach ($days as $day) {
    $week[$day->format('w')] = array('format' => $day->format('Y-m-d'), 'day' => $day);
}

获取当前周的所有记录,按天分组:

$sql = "
    SELECT COUNT(`booth_number`) AS booth, DATE_FORMAT(`Date`, '%w') AS day_of_week
    FROM `registration1`
    WHERE WEEK(`Date`, 3) = '{$start->format('W')}' AND `sold_by` = '{$_SESSION['user']}'
    GROUP BY day_of_week
";
$result = mysql_query($sql) or die('ERROR: ' . mysql_error());

将DB结果放入$week数组:

while($row = mysql_fetch_assoc($result)) {
    $week[$row['day_of_week']]['row'] = $row;
}

现在你有每日数组包含你想要的结果:

print_r($week);