2

我想从不同的数据表中绘制一些图表。数据表来自一个名为 ajax.php 的 PHP 文件中的查询。

这是代码:

//JOB POST BY SALARY
function jobsalary(){

global $DB;

//query by location total post
$sql = 'SELECT lcs.salaryrange_id, count(ljj.job_id) as count, lcs.salaryrange_item FROM {local_jobs_job} ljj INNER JOIN {local_cv_salaryrange} lcs ON ljj.job_salaryrangeid = lcs.salaryrange_id WHERE job_type = 0 GROUP BY lcs.salaryrange_item';


//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->salaryrange_item, '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);

}

 //JOB POST BY JOB'S CATEGORY
 function jobcategory(){

 global $DB;

 //query by job's category total post
 $sql = 'SELECT ljc.category_id, count(ljj.job_id) as count, ljc.category_group FROM {local_jobs_job} ljj INNER JOIN {local_jobs_category} ljc ON ljj.job_categoryid = ljc.category_id  where ljj.job_type = 0 group by ljc.category_group';


 //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->category_group, '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);


}

该图表是在另一个名为 report.php 的 php 文件中绘制的。这是代码:

 google.charts.setOnLoadCallback(drawItems);

    function drawItems(){
            var functionName = JSON.stringify({values:"joblocation"});
            var jsonPieChartData = $.ajax({
                            url: "ajax.php",
                            contentType: "application/json",
                            data: "functionName",
                            dataType: "json",
                            async: false,

                            }).responseText;

                var piechartdata = new google.visualization.DataTable(jsonPieChartData);

                var optionsChart = {
                                //title: 'Job Posts by Location',
                                pieSliceText: 'label',
                                tooltip: {isHtml: true},
                                width: 500,
                                height: 300,
                                chartArea: { left:"5%",top:"20%",width:"90%",height:"90%" }
                              };

                // Instantiate and draw our pie chart, passing in some options.
                var chart = new google.visualization.PieChart(document.getElementById('chart_div'));


                //draw the chart      
                chart.draw(piechartdata, optionsChart);

                    }

但是,我不知道如何使用 ajax 调用为每个图表调用特定函数。以前,我只是使用 ajax.php 文件中的一个数据表绘制一个图表。

4

1 回答 1

0

根据您的要求创建多个 ajax 文件并在一个 php 文件中调用所有 ajax 文件

于 2017-09-12T06:49:42.543 回答