문제

I'm trying to set up some simple Amcharts graphs for a company intranet webpage. Over the last two weeks I have created HTML/JS and produced a nice graphic using Amcharts (data hard-coded in the HTML for demo purposes). I also installed XAMPP and created a MySQL database, populated with tables and some data that gets imported from csv files.

So far, everything is working fine - I can display nice graphics and I can collect the data for supplying data to the graphs. However, I have been approaching this problem from the 2 ends (getting the source data into a database AND presenting the data in a graph on a webpage). Now I need to join these 2 ends, so I can feed Amcharts with data from MySQL.

I know I need to use PHP to get the data from MySQL and put this into an array that can be used by Amcharts but my PHP knowledge is very basic and I'm struggling with the code.

What I have is PHP code which successfully connects to MySQL and extracts data to display in a browser - that works. I just don't know how to get that data into a multi-dimensional array in the format that Amcharts needs for plotting its graph.

It would be really great if you guys could give me some help here and fill in the missing pieces. I have some pseudo code for the logic of creating the array as the basis for the 'real' php code.

This is the pseudo code for populating the array:

;charset=UTF-8', '', '', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

$stmt = $db->query("SELECT * FROM <mytable> WHERE <mycondition>");

$prevweek = "9999";
$headrowdone = 0;

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  if ($prevweek < $row['WeekNumber']) {
      // New week so write out Category X Label ('Week') with the week number of the following data rows
      chartData.push($DataRow);
      $headrowdone = 0;
  } 
  if (!$headrowdone) {
      $DataRow = "Week: "+$row['WeekNumber'];
      $headrowdone = 1;
  }
  // Write out the X-Axis Category value and the Y-Axis value
  $DataRow = $DataRow+$row['XAxisCategory']+": "+$row['YAxisCategory'];
  $prevweek = $row['WeekNumber'];
}
chartData.push($DataRow); ?>

The SQL table looks like:

CREATE TABLE ( WeekNumber varchar(4), XAxisCategory varchar(50), YAxisValue integer );

and has data like: '1301','A',10 '1301','B',20 '1301','C',24 '1302','A',11 '1302','B',22 '1302','C',27 '1303','A',14 '1303','B',23 '1303','C',28 ...etc

The data array for amcharts needs to look like:

var chartData = [{ Week: "1301", A: 10, B: 20, C: 24 }, { Week: "1302", A: 11, B: 22, C: 27 }, { Week: "1303", A: 14, B: 23, C: 28 ....etc }];

도움이 되었습니까?

해결책

// This is spoofing your fetch via pdo
$rows [] = array('WeekNumber'=>1301, 'A'=>10);
$rows [] = array('WeekNumber'=>1301, 'B'=>20); 
$rows [] = array('WeekNumber'=>1301, 'C'=>25);

$rows [] = array('WeekNumber'=>1302, 'A'=>12);
$rows [] = array('WeekNumber'=>1302, 'B'=>22); 
$rows [] = array('WeekNumber'=>1302, 'C'=>27);
//var_dump($rows);

// set up some vars
$lastweek = '';
$ctr = 0;
$data = array();


// loop thru the vars, build another array
foreach( $rows as $row){

if($row['WeekNumber'] !== $lastweek){
  $ctr++;
  $data[$ctr] = array('Week'=>$row['WeekNumber']);
  $lastweek= $row['WeekNumber'];
}
// this could be nicer, but for now will do
if( isset($row['A']) ) $data[$ctr]['A'] = $row['A'];
if( isset($row['B']) ) $data[$ctr]['B'] = $row['B'];
if( isset($row['C']) ) $data[$ctr]['C'] = $row['C'];

}

var_dump($data);

Then use json_encode() to get into the format you want.

This answer is a bit kludgy, but at least gets away from building strings to make json.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top