1

Hi I am trying to get data from MySql table and show the data in Highcharts by using PHP and Json I have try the example from internet and it is working fine but when I try to get data from my file it show nothing.

What I am trying to do: I am creating table of office attendance system and trying to show record day by day. So at y axis I want to count names of employees and x axis the date of attendance.

Problem: Nothing Is show from mu json.

Here is what my Json looks like:

[["Hamza","07\/04\/2014"],
["Junaid","07\/04\/2014"],
["Usman","07\/04\/2014"],
["Rajab","07\/04\/2014"],
["Hamza","08\/04\/2014"],
["Junaid","08\/04\/2014"],
["Usman","08\/04\/2014"],
["Rajab","08\/04\/2014"]]

I am having to value names and dates.

My PHP which is creating my Json code:

// Set the JSON header
header("Content-type: text/json");

$result = mysqli_query($con,"SELECT * FROM attendence");
$a=array();
while($row = mysqli_fetch_array($result)) {
 $row['name'] . "\t" . $row['date']. "\n";
 $b=array();
 array_push($b,$row['name']);
 array_push($b,$row['date']);
array_push($a,$b);
}
echo json_encode($a);

and this is my Jquery code:

$(function() {

    $.getJSON('highchart.php', function(data) {
        // Create the chart     
        $('#chart').highcharts('StockChart', {


            rangeSelector : {
                selected : 1,
                inputEnabled: $('#chart').width() > 480
            },

            title : {
                text : 'Attendence'
            },

            series : [{
                name : 'Empolyees',
                data : data,
                tooltip: {
                    valueDecimals: 2
                }
            }]
        });
    });

});
4
  • Your data doesn't make any sense. You have a name and a date, but no numeric y-value. Are you trying to show the count of those there on a particular day? Or how many days an individual was there in some time period? Commented May 16, 2014 at 12:59
  • this is a attendance table and everyday many employees have to add there time in and time out so in a single day there are many entries of a single day in my json code I only show one day attendance but there are many days in my table with same day. Commented May 16, 2014 at 13:42
  • you didn't answer my question. What value do you want your y axis to show? Commented May 16, 2014 at 14:43
  • sorry for late answer I want to count name and show it at y axis.! Commented May 17, 2014 at 9:32

3 Answers 3

1
  1. In the json_encode() you need to use JSON_NUMERIC_CHECK flag to avoid use string in your JSON
  2. In the highstock you need to have timestamps and value, not name and date.
  3. Dates should be timestamp, and as first element in array, not second as you have.
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to get a count of all people on a given day, you need to write a SQL query that returns that information. Assuming that date field is a datetime type and name is of varchar type:

SELECT COUNT(name) FROM attendence GROUP BY DATE(date);

Comments

0

perhaps you need to define what kind of data your x-axis will have
take a look at this fiddle (taken from highcharts website)

$(function () {
    $('#container').highcharts({
        xAxis: {
            type: 'datetime'
        },

        series: [{
            data: [
                [Date.UTC(2010, 0, 1), 29.9], 
                [Date.UTC(2010, 2, 1), 71.5], 
                [Date.UTC(2010, 3, 1), 106.4]
            ]
        }]
    });
});

2 Comments

but i want to keep dates dynamic? any idea for that?
@Hamza i don't think this is a prolem. Just look how x-axis is defined as datetime. Also, in this example the data are [date,string]. I have no familiarity with highcharts, but i thought the above example did exactly what you wanted

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.