PHP Array to JSON in this format as requested - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, January 27, 2022

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')),
                        },
                    }
                ];


source https://stackoverflow.com/questions/70809229/php-array-to-json-in-this-format-as-requested

No comments:

Post a Comment