CodeIgniter Forums
echo statements rendering out of order with multiple views? CodeIgniter newbie - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: echo statements rendering out of order with multiple views? CodeIgniter newbie (/showthread.php?tid=18355)



echo statements rendering out of order with multiple views? CodeIgniter newbie - El Forum - 05-04-2009

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


echo statements rendering out of order with multiple views? CodeIgniter newbie - El Forum - 05-04-2009

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

http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

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


echo statements rendering out of order with multiple views? CodeIgniter newbie - El Forum - 05-04-2009

[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


echo statements rendering out of order with multiple views? CodeIgniter newbie - El Forum - 05-04-2009

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


echo statements rendering out of order with multiple views? CodeIgniter newbie - El Forum - 05-04-2009

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


echo statements rendering out of order with multiple views? CodeIgniter newbie - El Forum - 05-04-2009

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


echo statements rendering out of order with multiple views? CodeIgniter newbie - El Forum - 05-04-2009

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

Smile