Welcome Guest, Not a member yet? Register   Sign In
table object outputting before things that were called prior
#1

[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');
}
}
#2

[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.
#3

[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; ?>
#4

[eluser]Aken[/eluser]
Better!




Theme © iAndrew 2016 - Forum software by © MyBB