drc4925 2016-03-19 14:48
浏览 39
已采纳

CodeIgniter和Google图表 - 基于mysql值的下拉列表

I am using code igniter, google charts with php and MySQL to display charts. It works using fixed query. I am trying to add a dropdown to display the chart based on the option (sql column "status") selected Here is what I have so far. How can I modify this to accept dropdown values?

model.php

public function get_chart_data()  
{
    $query = $this->db->get($this->db_mgmt);
    $this->db->select('rating, COUNT(rating) AS Count');
    $this->db->from('db_mgmt');
    $this->db->where('status =', $status);
    $this->db->group_by('rating'); 
    $query = $this->db->get();
    $results['chart'] = $query->result();
}

controller.php

$this->load->model('model', 'chart');
public function index() {
        $results = $this->chart->get_chart_data();
        $data['chart'] = $results['chart'];
        $this->load->view('index.php', $data);
}

view.php

<?php 
foreach ($chart as $object) {
$open_all[] = "['".$object->rating."', ".$object->Count."]";
}
?>

  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart_open);

   function drawChart_open() {
    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Rating');
    data.addColumn('number', 'Count');
    data.addRows([
    <?php echo implode(",", $open_all);?>
    ]);

    var options = {
      pieSliceText: 'value-and-percentage',
    };

    var chart = new google.visualization.PieChart(document.getElementById('open_div'));
    chart.draw(data, options);

  }

  <div id="open_div" class="chart"></div>

Thanks in advance!

UPDATE:

I have tried the below using ajax but it doesn't seem to work. I am definitely sure I am doing something wrong here but not sure where. Using Inspect in chrome also doesn't give any errors.

model.php

public function fetch_result($status)  
  {
    $query = $this->db->get($this->db_mgmt);
    $this->db->select('rating, COUNT(status) AS Status_Count');
    $this->db->from('db__mgmt');
    $this->db->where('status =', $status);
    $this->db->group_by('rating'); 
    $query = $this->db->get();
    return $query;
  }

controller.php

$this->load->model('model', 'chart');
public function mychart() {

if(!empty($_POST["val"])) {
    $val=$_POST["val"];
    $result_new=$this->chart->fetch_result($val);

    $array = array();
    $cols = array();
    $rows = array();
    $cols[] = array("id"=>"","label"=>" Rating","pattern"=>"","type"=>"string");
    $cols[] = array("id"=>"","label"=>"Count","pattern"=>"","type"=>"number");  

    foreach ($result_new as $object) {
      $rows[] = array("c"=>array(array("v"=>$object->risk_rating,"f"=>null),array("v"=>(int)$object->Status_Count,"f"=>null)));
   }

    $array = array("cols"=>$cols,"rows"=>$rows);
    echo json_encode($array);

    }

}

view.php

function drawChart_open_all(num) {         
    var PieChartData = $.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>" + "dashboard/chart/mychart",
    data:'val='+num,
    dataType:"json"
    }).responseText;

    alert(PieChartData);


    // Create the data table.
    var data = new google.visualization.DataTable(PieChartData);
    var options = {
              pieSliceText: 'value-and-percentage',       
    };

    var chart = new google.visualization.PieChart(document.getElementById('open_new'));
    chart.draw(data, options);

  }

<div><span> <b>Pie Chart<br /><br /></span></div>
<form>
  <select name="status" onchange="drawChart_open_all(this.value)">
<option value="WIP">WIP</option>
<option value="Close">Close</option>
  </select>
</form>
<div id="open_new" class="chart"></div>

Thanks in advance!!

  • 写回答

2条回答 默认 最新

  • douyang5943 2016-03-23 16:03
    关注

    I managed to figure out what the problem was and used ajax in the end. @WhiteHat solution led to also in the right direction. Thanks for that!

    model.php

    public function fetch_result($status)  
      {
        $query = $this->db->get($this->db_mgmt);
        $this->db->select('rating, COUNT(status) AS status_count');
        $this->db->from('db_mgmt');
        $this->db->where('status =', $status);
        $this->db->group_by('rating'); 
        $query = $this->db->get();
    
        $results_new = $query->result();  // <-- Forgot to add this!
        return $results_new;
      }
    

    controller.php

    $this->load->model('model', 'chart');
    
    public function mychart() {
    
    if(!empty($_POST['option'])) {     
    
    $val = $_POST['option'];            
    $result_new=$this->chart->fetch_result($val);
    
    $array = array();
    $cols = array();
    $rows = array();
    $cols[] = array("id"=>"","label"=>" Rating","pattern"=>"","type"=>"string");
    $cols[] = array("id"=>"","label"=>"Count","pattern"=>"","type"=>"number");  
    
    foreach ($result_new as $object) {
      $rows[] = array("c"=>array(array("v"=>(string)$object->rating),array("v"=>(int)$object->status_count)));
    }
    
    $array = array("cols"=>$cols,"rows"=>$rows);
    echo json_encode($array);
    
    }
    }
    

    view.php

       function drawChart_open_all(status) {
    
        var PieChartData = $.ajax({
        type: 'POST',
        url: "<?php echo base_url(); ?>" + "dashboard/chart/mychart",
        data: { 'option':status },  // <-- kept as option instead of val
        dataType:"json",
        global: false,              // <-- Added 
        async:false,                // <-- Added 
        success: function(data){    // <-- Added 
        return data;                // <-- Added 
        },
        error: function(xhr, textStatus, error){
            console.log(xhr.statusText);
            console.log(textStatus);
            console.log(error);
        }
        }).responseText;
    
        // Create the data table.
        var data = new google.visualization.DataTable(PieChartData);
        var options = {  pieSliceText: 'value-and-percentage', };
    
        var chart = new google.visualization.PieChart(document.getElementById('open_new'));
        chart.draw(data, options);
      }
    
    <div><span> <b>Pie Chart<br /><br /></span></div>
    <form>
    <select name="status" onchange="drawChart_open_all(this.value)">
    <option value="WIP">WIP</option>
    <option value="Close">Close</option>
    </select>
    </form>
    <div id="open_new" class="chart"></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题