0

new to this and (almost) desperate. in below code i want to set $rows1['date'][] = $data['tanggal']; data as X-Axis in highchart :

 $(function () {
    var chart;
    $(document).ready(function() {
    getAjaxData(1);
    var val =  location.search.split('proyek=')[1]
    getAjaxData(val);  
    function getAjaxData(proyek){
    $.getJSON("src/json/data.php", {proyek: proyek}, function(json) {   
    chart = new Highcharts.Chart({
    chart: {
        renderTo: 'scurve-proyek',
        type: 'column'                    
    },
    title: {
        text: ''
    },
     credits: {
      enabled: false,
    },
    subtitle: {
        text: ''
    },
    yAxis: {
        min: 0,
        max: 100,
        tickInterval: 20,
        title: {
            text: ''
            },
    },
    xAxis: {
       type: 'datetime',
       labels: {
            formatter: function ( ){
                return Highcharts.dateFormat('%Y-%m-%d', this.value);
            },
        },
    },
    tooltip:{
         formatter: function() {
            return '<b>' + this.series.name + '</b><br/>' +
                Highcharts.numberFormat(this.y, 2) +' %';
          },
    },

    plotOptions: {
         column: {
            dataLabels: {
            enabled: true,
            format: '{point.y:,.2f}'+'%',
            }   
     },
    },
    series: json
    });
    });  
    };     
    });
});

my data.php :

<?php
header("Content-type: application/json");
require_once "database.php";
$db = new database();
$mysqli = $db->connect();
$proyek = $_GET['proyek'];


$sql = "SELECT fren_pr FROM data_proyek WHERE kode_proyek = '$proyek'";
$rows = array();
$rows['name'] = 'Rencana';
$rows['color'] = '#50B432';
$result = $mysqli->query($sql);
while ($data = $result->fetch_assoc()) {
    $rows['data'][] = array($data['fren_pr'],);
}


$sql = "SELECT freal_pr, tanggal FROM data_proyek WHERE kode_proyek = '$proyek'";
$rows1 = array();
$rows1['name'] = 'Realisasi';
$result = $mysqli->query($sql);
while ($data = $result->fetch_assoc()) {
    $rows1['data'][] = $data['freal_pr'];
    $rows1['date'][] = $data['tanggal'];
}

$rslt = array();
array_push($rslt, $rows);
array_push($rslt, $rows1);
print json_encode($rslt, JSON_NUMERIC_CHECK);
$mysqli->close();

this is my json view :

enter image description here

I've spent a lot of time trying to solve it but haven't found a solution until now.

Is there an error in my php code?

hope someone will be kind enough to help, Thanks in advance.

Identic 7
  • 37
  • 4
  • 2
    What exactly happens when you run it now? Have you done any debugging? Try to narrow down the issue – ADyson Aug 18 '22 at 14:38
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Aug 18 '22 at 14:46
  • Do you get any errors? How your json looks like? – magdalena Aug 19 '22 at 08:26
  • hello @magdalena i have edited my question, i added a json data view that i created, the problem is what way i can do to be able to set the **date** data there as x-axis. thank you for answering my question. – Identic 7 Aug 19 '22 at 14:49
  • thanks @ADyson, from my question above, I just don't know what kind of way I can do to be able to set the x-axis of the **date** data in my json file. – Identic 7 Aug 19 '22 at 14:52
  • Hi @Identic7, thanks for the update! Before using your JSON as a series, you need to parse it to the relevant format. Check available formats for column series: https://api.highcharts.com/highcharts/series.column.data – magdalena Aug 22 '22 at 16:38

0 Answers0