Codeigniter View and echo

2019-03-29 17:58发布

问题:

I have a function that which process the side bar of a web page in codeigniter.

as follows :

function process_sidebar()
{
$this->load->view("first_access"); // ------------(1)
$this->load->view("second_access");// --------------(2)
echo "Here i want to show some data after loading view second_access"; //pls note here --(3)

$this->load->view("third_access"); // --------------------(4)
$this->load->view("fourth_access"); //-------------------------(5)

}

Please check the order numbers,but the problem is codeigniter not keeping the order.

it rendering the view last and showing the echo part first..

how can i overcome this ?

Thank you.

回答1:

You'll want to append_output() rather than echo:

<?php
function process_sidebar()
{
    $this->load->view("first_access"); // ------------(1)
    $this->load->view("second_access");// --------------(2)
    $this->output->append_output("Here i want to show some data after loading view second_access"); //pls note here --(3)
    $this->load->view("third_access"); // --------------------(4)
    $this->load->view("fourth_access"); //-------------------------(5)
}


回答2:

That's how view works in CodeIgniter, it doesn't get displayed immediately but gets buffered instead and displayed together later. To overcome, if the text you want to display in between is static, make it part of the 2nd or 3rd view. Otherwise, make it a variable of 2nd or 3rd view, and pass the text as view variable.