adhenurcahya28
adhenurcahya28

Reputation: 39

Get the JSON encode result from the Controller to View

I want to put a json encoded result (of ManagementController.php, statisticAction) to the Highchart syntax in view file (stat.phtml) in my Phalcon project. Was originally, in stat.phtml I used:

            ...
            ...
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: []
            }]
        } 
       $.getJSON('data/user_type.php', function(json) {
       options.series[0].data = json;
       chart = new Highcharts.Chart(options);
    });
});

with the data/user_type.php placed in public folder.

user_type.php:

<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
   die('Could not connect: ' . mysql_error());
}
mysql_select_db("mockup_workspace", $con);
$result = mysql_query("SELECT name_type_user, total FROM v_ntype_user where id_admin=1");

$rows = array();
while($r = mysql_fetch_array($result)) {
$row[0] = $r[0];
$row[1] = $r[1];
array_push($rows,$row);
}
print json_encode($rows, JSON_NUMERIC_CHECK);

mysql_close($con);
?>

It works when I load, although the data is static. But it still uses native PHP syntax, so I want to use that logic in Managementcontroller. Then I found the issue that $.getJSON that still need URL json file (like data/user_type.php), while I was using ManagementController (myController/actionController). So, what if I want to bring it from the Controller?

ManagementController.php

<?php

 namespace workspace_mockup_2\Controllers;
 use workspace_mockup_2\Models\VnTypeUser as nTypeUser;
 use Phalcon\Mvc\Controller;

 class ManagementController extends Controller {
  ...
  public function statisticAction() {
    $id_admin = $this->session->get('auth');

    // User Type
    $typeUser = nTypeUser::find('id_admin="' . $id_admin . '"');    
    $rows = array();
    foreach($typeUser as $t) {
        $row[0] = $t->name_type_user;
        $row[1] = $t->total;
        array_push($rows,$row);
    }
    echo json_encode($rows, JSON_NUMERIC_CHECK);
    $this->view->pick("/frontend/user_management_page/stat");
}

I've been looking for a way, such put this in that Controller,

...
$printjson = json_encode($rows, JSON_NUMERIC_CHECK);
$this->view->printjs = $printjson;

and put $printjs variable to replace $.getJSON('data/user_type.php', ... in stat.phtml, but it did not work as well. Please help :(

Thank's..

Upvotes: 2

Views: 5998

Answers (1)

Nikolaos Dimopoulos
Nikolaos Dimopoulos

Reputation: 11485

If you are using an ajax call then you need to return back the results from your controller with an echo or a print. If not, then you need to set your variable (that contains the data) in the view.

Examples: Controller - set a variable in the view

namespace workspace_mockup_2\Controllers;
use workspace_mockup_2\Models\VnTypeUser as nTypeUser;
use Phalcon\Mvc\Controller;

class ManagementController extends Controller
{

    public function statisticsAction()
    {
        // User Type
        $typeUser = nTypeUser::find('id_admin="' . $id_admin . '"');
        $rows = array();
        while ($r = mysql_fetch_array($typeUser)) {
            $row[0] = $r[0];
            $row[1] = $r[1];
            array_push($rows,$row);
        } 
        $this->view->pick("/frontend/user_management_page/stat");
        $this->view->setVar('data', json_encode($rows, JSON_NUMERIC_CHECK));
    }
}

View

options.series[0].data = <?php echo data; ?>;
chart = new Highcharts.Chart(options);

Controller - return data as AJAX

namespace workspace_mockup_2\Controllers;
use workspace_mockup_2\Models\VnTypeUser as nTypeUser;
use Phalcon\Mvc\Controller;

class ManagementController extends Controller
{
    public function statisticsaction()
    {
        $this->view->setRenderLevel(PhView::LEVEL_ACTION_VIEW);
        $this->response->setContentType('text/json');

        // User Type
        $typeUser = nTypeUser::find('id_admin="' . $id_admin . '"');
        $rows = array();
        while ($r = mysql_fetch_array($typeUser)) {
            $row[0] = $r[0];
            $row[1] = $r[1];
            array_push($rows,$row);
        } 
        // This will return raw JSON suitable for AJAX calls
        echo json_encode($rows, JSON_NUMERIC_CHECK));
    }

}

View

            ...
            ...
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: []
            }]
        } 
       $.getJSON('management/statistics', function(json) {
       options.series[0].data = json;
       chart = new Highcharts.Chart(options);
    });
});

NOTE: Please remove all references to mysql_* commands and either replace them with PDO or mysqli or even Phalcon's Models. mysql_* has been deprecated

Upvotes: 1

Related Questions