Arasoi
Arasoi

Reputation: 35

Codeigniter: Unable to echo data

Ok folks,

I have an odd issue with a function of mine.

public function getOutages($Site)
{
    // pull a json data dump of all outages 
    If(!$Site){
        echo '[{}]';
    }else{
        $this->load->database('default', TRUE);
        $this->db->where('Clear', '0');
        $this->db->where('FracID', $Site);
        $query = $this->db->get('vw_Outages');

        echo json_encode($query->result_array());
    }
}

This when accesed directly will not echo anything. By enabling the profiler though it functions fine and outputs the data.

public function getOutages($Site)
{
$this->output->enable_profiler(TRUE);
    // pull a json data dump of all outages 
    If(!$Site){
        echo '[{}]';
    }else{
        $this->load->database('default', TRUE);
        $this->db->where('Clear', '0');
        $this->db->where('FracID', $Site);
        $query = $this->db->get('vw_Outages');

        echo json_encode($query->result_array());
    }
}

Any insight into this would be more then welcome :D .

Upvotes: 0

Views: 2384

Answers (2)

Francis Avila
Francis Avila

Reputation: 31621

CodeIgniter has an output buffering system (which also allows it to do things like cache controller output, set headers, and collect view output). You don't usually echo from a controller method. Do this instead:

public function mymethod() {
    $anobject = array();
    $output = json_encode($anobject);

    $this->output->set_content_type('application/json');
    $this->output->set_output($output);
}

See the CodeIgniter documentation for the Output class.

Upvotes: 7

JanLikar
JanLikar

Reputation: 1306

Maybe you have output buffering or compression enabled-this could cause problems like this. Also check that the variable you're trying to output isn't empty. If this doesn't help, try using a view to display data.

Upvotes: 0

Related Questions