Welcome Guest, Not a member yet? Register   Sign In
echo statements rendering out of order with multiple views? CodeIgniter newbie
#1

[eluser]Unknown[/eluser]
Hi I'm new to codeigniter and I have been trying to to make a main controller and instantiate other controllers like a menu controller, and then display the views, but the output seems to go out of order. Mainly echo statements seem to come out first.
Anyone know why echo statements are rendered first?

In my main controller I have:

Code:
$this->load->view('header',$header);
    require_once('menu.php');
    $MainMenu = new Menu();
    $MainMenu->showMenu();

Here's the code for showMenu, where the echo statements are.

Code:
function showMenu(){
        
             echo "fun in the sun";
             $this->load->library('calendar');
             echo $this->calendar->generate(); echo seems to render wrong time
                        
            $menuItems['items'] =  this->menu_model->getMenuItems();        
            $this->load->view('menu_view',$menuItems);
            

            
        }

I thought I would get

header view
fun in the sun
calendar

menu view

instead I get:


fun in the sun
calendar

header view
menu view

the two echo statements output first even though they are called second.

Any help is appreciated.
Thanks so much.
#2

[eluser]Thorpe Obazee[/eluser]
You should be loading libraries like:

http://ellislab.com/codeigniter/user-gui...aries.html

Anyway, use the views to output anything to the browser.
#3

[eluser]Dam1an[/eluser]
The reason for this, is that an echo is sent directly to the browser, but when you load a view, it gets buffered until you read the end of that controller function, then all the views which have been buffered get written to the screen at once
#4

[eluser]TheFuzzy0ne[/eluser]
Output should only be sent from a view, unless of course you're debugging.
#5

[eluser]Unknown[/eluser]
Thanks all. Super fast response!

Ya I woke up this morning and realized that I was calling the echo statements in my menu controller not the view, and thus it wasn't being handled as part of the view. (Good to know the view buffer stream is cleared at the end of the controller function).

I passed the calendar generate() output as a string to the view and it works perfectly Smile


Thanks again.
#6

[eluser]TheFuzzy0ne[/eluser]
Oh, and welcome to the CodeIgniter forums. Big Grin
#7

[eluser]Thorpe Obazee[/eluser]
[quote author="JojoBelmonte" date="1241476178"]Thanks all. Super fast response![/quote]

Smile




Theme © iAndrew 2016 - Forum software by © MyBB