I want to draw a google chart, the query is written in a function in ajax.php file. The chart is in different php file calling the json data using ajax.
I want to call a function in php file, joblocation().
function joblocation(){
global $DB;
//query by location total post
$sql = 'SELECT ljl.location_id, count(ljj.job_id) as count, ljl.location_name FROM {local_jobs_job} ljj INNER JOIN {local_jobs_location} ljl ON ljj.job_location = ljl.location_id WHERE ljj.job_type = 0 GROUP BY ljl.location_name';
//get the query into record
$data = $DB->get_records_sql($sql);
//put the query into array
$rows = array();
$rows = array_map(function($item) {
return (object) ['c' => [
(object) ['v' => $item->location_name, 'f' => null],
(object) ['v' => intval($item->count), 'f' => null]
]];
}, array_values($data));
// prepare return data
$cols = [
(object) ['id' => '', 'label' => 'LABEL', 'pattern' => '', 'type' => 'string'],
(object) ['id' => '', 'label' => 'TOTAL', 'pattern' => '', 'type' => 'number'],
];
$returndata = new stdClass;
$returndata->cols = $cols;
$returndata->rows = $rows;
echo json_encode($returndata);
}
This is the ajax call to call the function.
var jsonColumnChartData = $.ajax({
url: "ajax.php",
contentType: "application/json",
data: {action: 'joblocation'},
dataType: "json",
async: false
}).responseText;
Is it correct to do this way?
I tried this:
var jsonPieChartData = $.ajax({
url: "ajax.php",
contentType: "application/json",
data: {},
dataType: "json",
async: false,
success: function(result){
joblocation(result);
},
error: function() {
alert('Error occured');
}
}).responseText;
When I run, the error pop up appear. It doesn't get the function.