Requesting data

Hi,I am trying to create a graph using chartjs,I am still new on this creating graph and i think i am in big trouble,what i want to achieve is that i am going to get all the number registered employee in each month from jan-dec. and to draw it to a graph with the use of jquery.Ajax,…my question on this do i need to create 12 php script for counting the registered employe in each month ?and how do i set them to the graph ?

This is my code please correct me if i am wrong.

    type: 'post',
    url : 'countemployee.php',
    success: function(data){
      jan = data.january;
      feb = data.february;


       //query for the mont of january   
       $connectionString = $db->prepare("select (*) as count from employee   
                                     where date_hired > = '2013-01-01' AND date_hired <= '2013-01-31'");

         $jan = $sql->fetchColumn();

  //log error message here.

        //another count query for month of february
  //log error message here.

        //another count query for month of march
  //log error message here.


encode to json

$month = array("$january"=>'$jan',


<!DOCTYPE html>
    <meta name="viewport" content="initial-scale = 1, user-scalable = no">
    <script src="Chart.js-master/Chart.js"></script>
<canvas id="myChart" height="450" width="600"></canvas>

<script type="text/javascript">

    var data = {
        labels: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
        datasets: [
                fillColor: "rgba(220,220,220,0.5)",
                strokeColor: "rgba(220,220,220,1)",
                data: jan,feb,etc..


    var ctx = document.getElementById("myChart").getContext("2d");

    new Chart(ctx).Bar(data);




I would think that someone from the PHP forum will be better placed to answer this part of things. Moving the thread over there now.

Hi Jemz,

Rather than doing separate queries for each month, you can return all the data with one query like this:

SELECT COUNT(*) as hired, MONTHNAME(date_hired) as month 
FROM employees 
GROUP BY MONTH(date_hired)

Then use PDO’s fetchAll method to return all the rows as an array, which you can loop over and put into an associative array to be encoded as JSON and sent back to the browser:

$rows = $sql->fetchAll();

$results = array();
foreach ($rows as $row) {
    $results[$row['month']] = $row['hired'];

echo json_encode($results);

You may need to add a WHERE clause to make sure you only get data for the current year instead of ALL years that may be stored in the database. Or add Year to your SELECT and GROUP BY so you can show all months over each year.

Hi fretburner, Thank you for this… is this a function “MONTHNAME”,okay how do i put the where clause of the year just like cpradio says.?

Yes, MONTHNAME is a MySQL function - you can find out more in the manual:

To limit the query to a specific year, you would do something like this:

WHERE YEAR(date_hired) = '2013'

Hi fretburner, Thank you so much it’s working now,thank you for helping me :slight_smile:

Hi fretburner, Is it possible to encode in json with 2 arrays ?


you’d want to put them in another array

json_encode(array('firstArray' => $array1, 'secondArray' => $array2));

Hi cpradio,

Thank you for this :slight_smile: