score:1
Here is a working example of how you can format the PHP into JSON. This does not include the mysql part, but that should not be hard to figure out.
Updated 2017/10/12, corrected mistake with double data:
Updated 2017/10/16, corrected mistake without array push:
<?php
$males = array();
$malepersons = array();
$females = array();
$femalepersons = array();
$result = array();
$values = [[
"name" => 'Anna',
"height" => 175,
"weight" => 53.4,
"gender" => 'Female'
],[
"name" => 'Dan',
"height" => 185.1,
"weight" => 90.4,
"gender" => 'Male'
] ];
//Simplified for loop
foreach ($values as $row){
$person = array();
$person['name'] = $row['name'];
$person['x'] = $row['height'];
$person['y'] = $row['weight'];
if($row['gender'] == 'Male'){
array_push($malepersons, $person);
} elseif($row['gender'] == 'Female') {
array_push($femalepersons, $person);
}
}
//took these out of the for loop, only need to be set once
$males['color'] = 'black';
$males['name'] = 'Male';
$males['data'] = $malepersons;
$females['color'] = 'green';
$females['name'] = 'Female';
$females['data'] = $femalepersons;
array_push($result, $males);
array_push($result, $females);
print json_encode($result, JSON_NUMERIC_CHECK);
?>
This will give you JSON that looks like this:
[{
"color": "black",
"name": "Male",
"data": [{
"name": "Dan",
"x": 185.1,
"y": 90.4
}]
}, {
"color": "green",
"name": "Female",
"data": [{
"name": "Anna",
"x": 175,
"y": 53.4
}]
}]
Which is what highcharts wants as series input.
In your case, the $values
array is actually the data returned by SQL, you just need to change the loop to loop what you want it to loop.
Added working example: https://ideone.com/RPp3DO
PHP DOCS on keyed arrays: http://php.net/manual/en/language.types.array.php
Source: stackoverflow.com
Related Query
- Highcharts Mysql Json arrays PHP
- Highcharts Mysql Json arrays PHP
- Highcharts drilldown json from php mysql
- PHP mySQL Highcharts Json Multiselect
- Formatting JSON data monthwise for HighCharts using MySQL
- PHP JSON Highcharts load database result
- Highchart - Display JSON Data - MYSQL / PHP
- Highcharts multiple series json from php
- Highcharts json php multiple series
- HIghCharts PHP MySQL
- JSON data not showing in highstock Candlestick chart using PHP & MYSQL
- passing json values to highcharts from .net code behind
- passing formatting JavaScript code to HighCharts with JSON
- How to send json to highcharts from php
- Generating Highcharts from MySQL data with PHP does not work
- Simple, Open source PHP wrapper for Highcharts library
- HighCharts - making a Pie with MySQL and PHP
- Having trouble JSON encoded data from MySQL through Highcharts
- Generate JSON From Mysql Using PHP
- Converted PHP code that built an array to JS and now highcharts doesn't work - what did I do wrong?
- PHP + mysql + highcharts
- Highcharts using JSON - graph not displaying mysql data
- MSSQL Server to PHP Array to JSON Encode to Highcharts
- HighCharts Bubble graph JSON as data source
- How do I get DateTime from mysql and set it to json format ready for highcharts
- Plotting multiple series from JSON php import to Highcharts
- HighCharts data defined in a HTML Table using PHP MySQL
- Highcharts not displaying json data in php
- use multiple json calls for drawing multiple lines on highcharts php
- Highcharts not being drawn in some cases when using data from MySQL (page elements created before database query complete?) PHP / MySQL
More Query from same tag
- highcharts pie chart multiple sections selection
- Passing string data as categories to Highcharts
- Two y Axis in Highcharter in R
- Highcharts mulitple y axis unit not display
- how to : specify colors for multiple axes chart?
- Dual x-axis on same line with Highcharts. Possible?
- How to display legend in Highcharts Waterfall Chart and make the sum column of waterfall appear in multiple colors?
- Allow text input (write-back) from highchart tooltip
- Highcharts: Multiple vertical charts
- Highcharts: Additional legend items to control multiple items
- How to change time in hours on HighChart xAxis from Military to Standard time?
- a website that loads a lot of objects - remote proxy?
- react-hightchart: all values in event.point are null
- Can I use two "categories" arrays in a stacked bar chart?
- Can we create pie chart and scatter plot combined using highcharts.js?
- reverse highcharts heatmap legend
- get bot xAxis values when doing click event on highcharts
- HighMaps - need to make datalabels clickable
- How to make highcharts stacked bar chart start from a specific date
- Highcharts: Show tooltip anywhere over series value column without using shared:true?
- spline chart highchart to fill the color in series
- Show specific series values in the stack label using highcharts
- Replace highcharts with images for report?
- Highcharts - only show tooltip when hovering directly on point
- Problem injecting variable-pie chart with highcharts-export-server
- Incorrect Highcharts colorAxis range
- highcharts drilldown column chart change Aaxis lables
- How to convert a JavaScript object to array
- HighChart.js data source recommendations
- highstock ie8 error