CodeIgniter and Google charts - dropdown based on mysql values
<p>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? </p>

<p>model.php</p>

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

<p>controller.php</p>

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

<p>view.php</p>

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

  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([
    &lt;?php echo implode(",", $open_all);?&gt;
    ]);

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

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

  }

  &lt;div id="open_div" class="chart"&gt;&lt;/div&gt;
</code></pre>

<p>Thanks in advance!</p>

<p><strong>UPDATE:</strong></p>

<p>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.</p>

<p>model.php</p>

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

<p>controller.php</p>

<pre><code>$this-&gt;load-&gt;model('model', 'chart');
public function mychart() {

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

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

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

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

    }

}
</code></pre>

<p>view.php</p>

<pre><code>function drawChart_open_all(num) {         
    var PieChartData = $.ajax({
    type: "POST",
    url: "&lt;?php echo base_url(); ?&gt;" + "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);

  }

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

<p>Thanks in advance!!</p>