'PHP Array to JSON in this format as requested
I need to make a php array from a mysql request, and in a loop pass into a JSON.
My sql is like this below:
$arDados = array();
$result = $conexao->sql("SELECT * FROM sys_fabrica WHERE fl_ativo = 1 ORDER BY tl_fabrica");
while($dados = mysql_fetch_array($result)) {
$cd_fabrica = $dados['cd_fabrica'];
$tl_fabrica = $dados['tl_fabrica'];
$tl_razao = $dados['tl_razao'];
$totalValorAno = retornaValorTotalAnoPecas($ano, $cd_fabrica);
$metaFaturamentoAnual = retornaMetaFaturamentoAnual($ano, $cd_fabrica);
$percentMetaFaturamentoAnual = number_format(($totalValorAno * 100) / $metaFaturamentoAnual, 0, ',', '.');
if($percentMetaFaturamentoAnual < 50){
$classeProgressFaturamento = "danger";
} else if($percentMetaFaturamentoAnual < 90){
$classeProgressFaturamento = "warning";
} else {
$classeProgressFaturamento = "success";
}
$arrayDados[] = array(
fabrica => $tl_fabrica,
value => abs($percentMetaFaturamentoAnual),
full => 100,
columnSettings => array(
fillOpacity => 1,
fill => "am5.color(KTUtil.getCssVariableValue('--bs-$classeProgressFaturamento'))";
)
);
}
$arrayDados = json_encode($arrayDados, JSON_PRETTY_PRINT);
echo $arrayDados;
And this is the data I need to pass into thee graphics (AMCharts).
// Data
var data = [
{
fabrica: "Fabrica",
value: 80,
full: 100,
columnSettings: {
fillOpacity: 1,
fill: am5.color(KTUtil.getCssVariableValue('--bs-info')),
},
}
];
Solution 1:[1]
Thank you everyone for the help. This is how I solved.
I worked it out using XML and pushing data into js object.
$.ajax({
type: "GET",
url: "<?=SITE_PATH?>/studio/assets/ajax/index_data_grafico_meta_ano.php?ano=<?=$ano?>&codFabrica=<?=$codFabrica?>",
dataType: "xml",
success: function(xml) {
var data = [];
$(xml).find('item').each(function(index){
var codFabrica = $(this).find('codigo').text();
var fantasia = $(this).find('fantasia').text();
var razao = $(this).find('razao').text();
var meta = $(this).find('meta').text();
var classe = $(this).find('classe').text();
var colorGraph = KTUtil.getCssVariableValue('--bs-'+classe);
colorGraph = am5.color(colorGraph);
var columnSettings = {};
columnSettings = {
fillOpacity: 1,
fill: colorGraph
}
data.push({
fabrica : fantasia,
value: meta,
full: 100,
columnSettings: columnSettings
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | marc_s |
