2

Ich verwende Code-Zünder, Google-Diagramme mit PHP und MySQL, um Diagramme anzuzeigen. Es funktioniert mit einer festen Abfrage. Ich versuche, ein Dropdown-Menü hinzuzufügen, um das Diagramm basierend auf der Option (SQL-Spalte "Status") ausgewählt Hier ist, was ich bisher habe. Wie kann ich dies ändern, um Dropdown-Werte zu akzeptieren?CodeIgniter und Google Charts - Drop-Down basierend auf Mysql-Werten

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> 

Vielen Dank im Voraus!

UPDATE:

Ich habe das versucht, unter Verwendung von AJAX, aber es scheint nicht zu funktionieren. Ich bin mir sicher, dass ich hier etwas falsch mache, aber nicht sicher wo. Die Verwendung von Inspect in Chrome gibt ebenfalls keine Fehler.

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> 

Vielen Dank im Voraus !!

+0

könnte einfacher sein, eine [Armaturenbrett] zu verwenden (https://developers.google.com/chart/interactive/docs/gallery/controls # dashboard) in Kombination mit einem [CategoryFilter] (https://developers.google.com/chart/interactive/docs/gallery/controls#categoryfilter) - lassen Sie Google die Filterung ... – WhiteHat

+0

@WhiteHat Vielen Dank für die Lösung i Es funktioniert super. Außerdem wollte ich fragen, ob ich den Wert senden wollte, um eine andere Funktion in model.php auszuführen und auf demselben Diagramm anzuzeigen. Zum Beispiel, wenn die Zeit geändert wurde, wollte ich eine andere MySQL-Abfrage für dieselbe Tabelle aufrufen, die eine andere Menge von Werten für die gleichen Spalten zurückgeben würde. –

+0

nicht viel auf PHP, ich würde Ajax von Javascript verwenden - aber scheint, wie Sie eine Anfrage senden könnte - oder wenn die Daten nicht massiv sind, mehr enthalten und werfen Sie einen DateRangeFilter ... – WhiteHat

Antwort

0

ich es geschaffen, zu senden, was das Problem war und verwendet Ajax am Ende. @WhiteHat Lösung führte auch in die richtige Richtung. Dank dafür!

Modell.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

ich denke, die einfachste Sache wäre eine GET-Anforderung mit dem <option> Wert

Zuerst zu schicken, um Ihre ersten Version zurückgehen.

Als nächstes senden, um den Wert in Ihrem onchange Ereignis

function drawChart_open_all(num) { 
    location = "<?php echo base_url(); ?>" + "dashboard/chart/mychart?option=" + num; 
} 

dann in Modell -
get_chart_data()

sollten Sie den Wert mit zugreifen können -
$_GET['option']

Verwenden Sie das, um Ihre Abfrage zu ändern

hier ist eine alte Antwort mit ähnlichem Konzept - Unterschied es POST verwendet vs. GET
und <form> mit einer <input type="submit"> Taste der Anforderung, um herauszufinden,
How to pass JavaScript variables to PHP?

+0

hoffe, das hilft, könnten Sie 'Ajax' verwenden, um den Anruf zu machen, anstatt mit 'location = ...' - aber ich denke das würde mehr Arbeit erfordern ... – WhiteHat