php - Insert correctly data with ajax for highcharts

421

i have a problem with highchart. I have to put some date from a database but i can't visualize the correct graph. I show you my full code and the img of the visualization that i have now. Please help me on that, i'm blocked from 3 days in this thing.

Well my js that inizialize the graph and call the ajax function is:

$(function () {
    var cs = document.getElementById('alunni').firstChild.nodeValue;


    var rend = $('#alunni')

    var options = {
        chart: {
            plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                renderTo: rend[0]
            },
            title: {
                text: 'Numero di studenti iscritti per ogni anno di corso'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Percentuale di alunni: ',
            }]
        };

        $.ajax({
            type: 'POST',
            url: 'alunnixclasse.php',
            async: false,
            data: {'cs': cs},
            dataType: 'json',
            success: function(data) {
                options.series[0].data = data;
            },
            error: function(data){ 
                alert("Chiamata fallita, si prega di riprovare...");
            }
        });

        console.log(options.series);

        var chart = new Highcharts.Chart(options);
});

And this is the php that i use to take the date:

<?php

    include "connect.php";

    $codscu = $_POST['cs'];

    $query = "SELECT * FROM alunni WHERE alunni.codscu = $codscu ";
    $query2 = "SELECT total.TotAlun FROM total WHERE total.codscu = $codscu ";

    $risultato = mysql_query($query);
    $risultato2 = mysql_query($query2);

    $data = "";

    $elem = mysql_fetch_array($risultato);
    $tot = mysql_fetch_array($risultato2);

    for ($i = 0; $i <5; $i++) {
        $anno = "";
        switch($i) {
            case 0: { $anno = "Primo anno"; $value = ($elem['anno_1'] * 100) / $tot['TotAlun']; break; }
            case 1: { $anno = "Secondo anno"; $value = ($elem['anno_2'] * 100) / $tot['TotAlun']; break; }
            case 2: { $anno = "Terzo anno"; $value = ($elem['anno_3'] * 100) / $tot['TotAlun']; break; }
            case 3: { $anno = "Quarto anno"; $value = ($elem['anno_4'] * 100) / $tot['TotAlun']; break; }
            case 4: { $anno = "Quinto anno"; $value = ($elem['anno_5'] * 100) / $tot['TotAlun']; break; }
        }

        if ($i != 4) {
            $data = $data."['".$anno."', ".number_format($value, 1,".","")."], ";
        } else {
            $data = $data."['".$anno."', ".number_format($value, 1,".","")."]";
        }
    }

    print json_encode($data);
?>

Can you help me please... I don't know where is my error!

People are also looking for solutions to the problem: php - update content in 2 browsers AFTER submit

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.