2017-09-12 3 views
2

私は異なるデータテーブルからいくつかのチャートを描きたいと思います。データシートは、ajax.phpという1つのPHPファイル内のクエリからのものです。どのように1つのPHPファイル内の異なるdatatableから複数のgooglechartを描画するには?

これはコードです:

//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ファイルに1つのデータテーブルを使用して1つのチャートを描画しています。

答えて

0

複数のajaxファイルを作成し、すべてのajaxファイルを1つのPHPファイルで呼び出します。

+0

複数のajax呼び出しの作成について理解しています。しかし、すべてのajaxファイルを呼び出すのはどういう意味ですか?それは1つのファイルだけです。しかし、ファイルには複数の機能があります。私は各AJAX呼び出しのために関数を呼び出す方法を知らない。 – joun

+0

AjaxでGETメソッドを使用し、url(ajaxの下)にパラメータを渡し、もう一方のサイドで、ajax.phpファイルのパラメータをチェックします。 – anand

+0

あなたは例を挙げることができますか?私は前にいくつか試してみましたが、うまくいかなかった – joun

関連する問題