CodeIgniter Forums
table object outputting before things that were called prior - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: table object outputting before things that were called prior (/thread-54133.html)



table object outputting before things that were called prior - El Forum - 08-23-2012

[eluser]php_princess[/eluser]
When I run this controller it outputs the table before outputting the header. Why is this?

Code:
class Home extends CI_Controller
{
   public function index()
{
  $logged_in = $this->session->userdata('logged_in'); //inserts true or false into variable
  
  $data['title'] = 'My Site Title Here';
  
  $this->load->view('templates/header', $data);
  $this->load->view('home/index', $data); // loads static content of page
      
  if (!$logged_in) //decides whether to display dynamic content
  {
   $this->load->library('table');
    
     $items= array(
   array('Name', 'Color', 'Size'),
   array('Fred', 'Blue', 'Small'),
   array('Mary', 'Red', 'Large'),
   array('John', 'Purple', 'Medium'),
   );
  
   print $this->table->generate($items);
  }
  
  $this->load->view('templates/footer');
}
}



table object outputting before things that were called prior - El Forum - 08-23-2012

[eluser]Aken[/eluser]
Because the print() function in your controller is outputting things before the Output class does, which is where your view content goes when you use $this->load->view(). You should do the $logged_in check in your view, not the controller.


table object outputting before things that were called prior - El Forum - 08-24-2012

[eluser]php_princess[/eluser]
How's this? I'm not printing from the controller anymore.

home controller
Code:
if (!$logged_in)
  {
   $this->load->library('table');
    
     $items= array(
   array('Name', 'Color', 'Size'),
   array('Fred', 'Blue', 'Small'),
   array('Mary', 'Red', 'Large'),
   array('John', 'Purple', 'Medium'),
   );
  
   $data['table'] = $this->table->generate($items);
   $this->load->view('home/dynamic', $data);
  }


home/dynamic.php
Code:
<?php print $table; ?>



table object outputting before things that were called prior - El Forum - 08-24-2012

[eluser]Aken[/eluser]
Better!