CodeIgniter Forums
[solved]header view won't display correctly - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [solved]header view won't display correctly (/showthread.php?tid=1468)



[solved]header view won't display correctly - naminator - 03-12-2015

This is like super easy question. There's a function in my controller which won't display correctly.
The header view will display below table which make characters unreadable due to missing encoding.
Please see code below. Please kindly advice what I missed.

Code:
public function show_registered(){
       $this->load->database();
       $this->load->helper('form');
       $this->load->helper('url');    
       $this->load->library('table');
       $this->load->helper('html');

       $this->load->view('template/header');
       
       $query = $this->db->get('registered');
       $data = array();
       $data[] = array('ลำดับที่','ภาพถ่าย');
       
       foreach($query->result() as $row){
           $image_properties = array(
               'src' => $row->photo,
               'width' => '180',
               'height' => '120',
           );

           $data[] = array($row->id,img($image_properties));
       }
       echo $this->table->generate($data);
       $this->load->view('template/footer');        
   }



RE: header view won't display correctly - InsiteFX - 03-12-2015

9 out of 10 times your missing an html closing tag in your template files some place. which you did not post here.


RE: header view won't display correctly - mwhitney - 03-12-2015

You should not have echo and calls to $this->load->view() within the same controller (in general, you should not use echo in a controller). Save the result of $this->table->generate($data) to a value which you can then pass to a view, or generate the table within the view.

To go into more detail: $this->load->view() buffers the output until your controller relinquishes control back to the framework, which also allows CI's Output class to perform some additional processing. By calling echo within the controller, you place data into the output stream outside of (before) the buffered views.

Libraries and/or helpers which generate portions of HTML are usually intended to be used within your views or to have their output passed to views via variables, not to generate output directly from your controllers.


RE: header view won't display correctly - CroNiX - 03-12-2015

CI's view output is buffered, so if you echo something from the controller it will appear BEFORE the rest of your view. As stated, don't echo from a controller. Instead that should be another view.