Welcome Guest, Not a member yet? Register   Sign In
Direct echo in Controller - bad parctice?
#7

(This post was last modified: 04-02-2015, 05:50 AM by RWCH.)

You can parse JSON data perfectly fine in a view, and there is no need to do it in a controller.  

Just pass your JSON string to the view like you normally do with data you are going to show.

In your controller:

PHP Code:
<?php 

$tennisArray 
= array('Djokovic' => 1'Federer' => 2'Nadal' => 3'Murray' => 4);
$data['tennisPlayers'] = json_encode($tennisArray);
$this->load->view("some_page"$data);

?>

In your view:

PHP Code:
<?php

echo $tennisplayers//your JSON encoded string 

?>

Which is similar to:

PHP Code:
<?php 

echo {"Djokovic":1,"Federer":2,"Nadal":3,"Murray":4};

?>

You can use this with AJAX or a normal page request and use it as you like.

If you want to send back XML or a PDF, you have to use another header (not HTML) and you cannot simply echo it in a view.
Reply


Messages In This Thread
RE: Direct echo in Controller - bad parctice? - by RWCH - 04-02-2015, 05:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB