Hi, i need call a controller action directly from javascript. I want connect charts of canvas.js and mysql ( https://canvasjs.com/forums/topic/how-can-i-use-php-mysql-dynamic-data/ ).
View:
<html><body><div id="chartContainer" style="width: 800px; height: 380px;"></div></body></html>
<script type="text/javascript">
$(document).ready(function () {
$.getJSON("data.php", function (result) {
var chart = new CanvasJS.Chart("chartContainer", {
data: [
{
dataPoints: result
}
]
});
chart.render();
});
});
</script>
Controller:
public function initialize()
{
$this->tag->setTitle('Estadisticas');
parent::initialize();
$this->assets
->collection('footer')->addJs('js/jquery.canvasjs.min.js');
}
public function indexAction()
{
$data_points = array();
$consulta = "SELECT COUNT(RE.reservacion_id) AS cantidad,RE.reservacion_nombre
FROM encuesta AS ENCUESTA, reservacion AS RE WHERE ENCUESTA.reservacion_id=RE.reservacion_id group by RE.reservacion_id";
$estadistica = $this->modelsManager->executeQuery($consulta);
foreach($estadistica as $p)
{
$point = array("label" => $p->reservacion_nombre , "y" => $p->cantidad);
array_push($data_points, $point);
}
echo json_encode($data_points, JSON_NUMERIC_CHECK);
}
how call to action rather File data.php