1

I have a task to solve how to change JSON encode to array string, look at my code below: I have

["11:00"]["09:00"]["01:00"]["12:00"]["11:00"]["10:00"]["01:00"]["00:00"]["23:00"]["22:00"]

how to change it to become

["11:00","09:00","01:00","11:00","10:00","01:00","00:00","23:00","22:00"]

My stats.php looks like this:

out .=  '         <div class="col-lg-5 col-md-5 col-sm-5 col-lg-offset-1 col-md-offset-1 col-sm-offset-1" style="height: 300px">';
        $out .=  '          <div class="chartjs-size-monitor">';
        $out .=  '              <div class="chartjs-size-monitor-expand">';
        $out .=  '                  <div class=""></div>';
        $out .=  '              </div>';
        $out .=  '              <div class="chartjs-size-monitor-shrink">';
        $out .=  '                  <div class=""></div>';
        $out .=  '              </div>';
        $out .=  '         </div>';
        $out .=  '         <canvas id="myChart2" class="canpas chartjs-render-monitor" style="display: block; width: 454px; height: 300px;" width="454" height="300"></canvas>';
        $out .=  '        </div>';

$graph_query = mssql_query("SELECT TOP 100 Convert(nvarchar,dtDate,108) AS Date, serial, nAverageUser, nMaxUser FROM tbl_ServerUser_Log ORDER BY serial DESC");

            $i = 1;
            while ($graph = mssql_fetch_array($graph_query)) {
                $graph['nMaxUser'] = round($graph['nMaxUser'] * $percent_inc);
                $graph['nAverageUser'] = round($graph['nAverageUser'] * $percent_inc);
                $serie1->addPoint(new Point($graph['Date'], $graph['nMaxUser']));
                $serie2->addPoint(new Point($graph['Date'], $graph['nAverageUser']));
                $date = date('H:i', strtotime($graph['Date']));
                $convert = json_encode(str_split($date, 5));
                // $convert = str_replace('][','',$convert);

                echo $convert;

            }

I want to show a chart and send the value in HTML. The HTML shall look like this:

<script type="text/javascript">
    var ctx = document.getElementById('myChart2').getContext('2d');
    var myChart2 = new Chart(ctx, {
        type: 'line',
        data: {
            labels: ['00:00','01:00','02:00','03:00','04:00','05:00','06:00','07:00','08:00','09:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00'], //Jamnya
            datasets: [{
                backgroundColor: 'rgba(232,72,163,0.2)',
                borderColor: '#e848a3',
                label: '29 May 2020',
                data: [807,657,600,578,565,576,611,855,625,573,571,584,607,647,771,943,920,647,622,608,722,832,902,1062],
                fill: true,
                pointRadius: 5,
                pointHoverRadius: 10,
                showLine: true
            }]
        }, options: {
            responsive: true,
            maintainAspectRatio: false,
            title: {
                display: true,
                text: 'Total Online Yesterday',
                position: 'top',
                fontSize: 15,
                fontStyle: 'bold'
            },
            legend: {
                display: false
            },
            elements: {
                point: {
                    pointStyle: 'circle'
                }
            },
            scales: {
                xAxes: [{
                    display: true,
                    scaleLabel: {
                        display: true,
                        labelString: 'TIME'
                    },
                    ticks: {
                        major: {
                            fontStyle: 'bold',
                            fontColor: '#FF0000'
                        }
                    }
                }],
                yAxes: [{
                    display: true,
                    scaleLabel: {
                        display: true,
                        labelString: 'TOTAL ONLINE'
                    }
                }]
            },
            tooltips:{
                callbacks: {
                    title: function(tooltipItem, data) {
                      return '29 May 2020 '+data['labels'][tooltipItem[0]['index']];
                    },
                    label: function(tooltipItem, data) {
                      return 'TOTAL : '+data['datasets'][0]['data'][tooltipItem['index']]+'';
                    },
                },
                titleFontSize: 15,
                bodyFontSize: 15,
                displayColors: false
            }
        }
    });
</script>

I just want to show my chart from manual value to query value that I catch from an SQL server.

Thanks in advance for answering my question.

1
  • 1
    Where is the original string coming from? Why can't you fix it to create proper JSON? Commented May 30, 2020 at 5:08

1 Answer 1

1

Don't echo JSON every time through the loop. Put the times in a single array, and echo the JSON at the end.

$dates = [];
while ($graph = mssql_fetch_array($graph_query)) {
    $graph['nMaxUser'] = round($graph['nMaxUser'] * $percent_inc);
    $graph['nAverageUser'] = round($graph['nAverageUser'] * $percent_inc);
    $serie1->addPoint(new Point($graph['Date'], $graph['nMaxUser']));
    $serie2->addPoint(new Point($graph['Date'], $graph['nAverageUser']));
    $date = date('H:i', strtotime($graph['Date']));
    $dates[] = $date;
    echo $convert;
}
echo json_encode($dates);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.