jeudi 9 février 2017

is there anything wrong with this javascript and php codes?

I was doing query from database. But the data result is incorrect from the database. my table of db is enter image description here

JAVASCRIPT CODE

//app.js
$(document).ready(function(){
    $.ajax({
        url: "http://localhost/chartjs/data.php",
        method: "GET",
        success: function(data) {
            console.log(data);
            var Gender = [];
            var jumlah = [];
            var Fakultas = [];
            var jumlah_orang = [];

            for(var i in data)
        {
            //Gender.push("Gender " + data[i].JenisKelaminID);
            if(data[i].JenisKelaminID == 1)
            {
                Gender.push("Men");
            } 
            if(data[i].JenisKelaminID == 2)
            {
                Gender.push("Women");
            }

            jumlah.push(data[i].jumlah);

            if(data[i].Fakultas == "A")
            {
                Fakultas.push("FAPERTA");
            }

            if(data[i].Fakultas == "B")
            {
                Fakultas.push("FKH");
            }

            if(data[i].Fakultas == "C")
            {
                Fakultas.push("FPIK");
            }

            if(data[i].Fakultas == "D")
            {
                Fakultas.push("FAPET");
            }

            if(data[i].Fakultas == "E")
            {
                Fakultas.push("FAHUTAN");
            }

            if(data[i].Fakultas == "F")
            {
                Fakultas.push("FATETA");
            }

            if(data[i].Fakultas == "G")
            {
                Fakultas.push("FMIPA");
            }

            if(data[i].Fakultas == "H")
            {
                Fakultas.push("FEM");
            }

            if(data[i].Fakultas == "I")
            {
                Fakultas.push("FEMA");
            }

            jumlah_orang.push(data[i].jumlah_orang);
        }
        Gender.push("Other");
        Fakultas.push("Other");
            var chartdata = {
                labels: Gender,
                datasets: [
                    {
                        label : 'Total',
                        backgroundColor: 
                        [
                            'rgba(79, 181, 59, 1)', //green for men
                            'rgba(239, 87, 196, 1)', //pink for women
                            'rgba(166, 160, 164, 1)', //grey for other
                        ],
                        borderColor: 'rgba(200, 200, 200, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: jumlah
                    }
                ]
            };

            var chartdata2 = {
                labels: Fakultas,
                datasets: [
                    {
                        label : 'Total',
                        backgroundColor: [
                            'rgba(79, 181, 59, 1)',
                            'rgba(239, 87, 196, 1)', 
                            'rgba(166, 160, 164, 1)', 
                            'rgba(96, 60, 64, 1)', 
                            'rgba(66, 16, 164, 1)', 
                            'rgba(177, 18, 74, 1)', 
                            'rgba(176, 60, 64, 1)', 
                            'rgba(186, 10, 64, 1)', 
],
                        borderColor: 'rgba(200, 200, 200, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: jumlah_orang
                    }
                ]
};

            var ctx = $("#mycanvas");
            var ctx2 = $("#mycanvas2");


            var barGraph = new Chart(ctx, {
                type: 'bar',
                data: chartdata

            });

            var barGraph2 = new Chart(ctx2, {
                type: 'bar',
                data: chartdata2

            });
        },
        error: function(data) {
            console.log(data);
        }
    });
});

PHP CODE

<?php
//setting header to json
header('Content-Type: application/json');

//database
define('DB_HOST', '127.0.0.1');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'dbintegrasi');

//get connection
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if(!$mysqli){
    die("Connection failed: " . $mysqli->error);
}

//query to get data from the table
$query = sprintf("SELECT JenisKelaminID, COUNT(JenisKelaminID) as jumlah FROM tahunmasukmagister GROUP BY JenisKelaminID");
$query2 = sprintf("SELECT Fakultas, COUNT(Fakultas) as jumlah_orang FROM s2view GROUP BY Fakultas");

//execute query
$result = $mysqli->query($query);
$result2 = $mysqli->query($query2);

//loop through the returned data
$data = array();

foreach ($result as $row){
    $data[] = $row;
}

foreach ($result2 as $row){
    $data[] = $row;
}

//freee memory associated with result
$result->close();
$result2->close();

//close connection
$mysqli->close();

//new print the data
print json_encode($data);

The chart result for "fakultas" came like this enter image description here

  [1]: http://ift.tt/2kV3iam

as we can see, in the table "Fakultas G" (FMIPA) is 3 but on the chart result Fakultas G is 3. What's wrong with those codes? Please help me to solve this. Thankyou




Aucun commentaire:

Enregistrer un commentaire