CodeIgniter Forums
Explantion of {elapsed_time} - 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: Explantion of {elapsed_time} (/thread-23927.html)



Explantion of {elapsed_time} - El Forum - 10-26-2009

[eluser]Grind[/eluser]
If you download Codeigniter and view the welcome_message.php file you see at the bottom of the page the following code:
Code:
Page rendered in {elapsed_time} seconds

Where does {elapsed_time} refer to? Is it possible to make such thing for displaying a guestbook, like {guestbook}?

Thanks.


Explantion of {elapsed_time} - El Forum - 10-26-2009

[eluser]rogierb[/eluser]
Check out the benchmarking class in the userguide. You will find the answer there.


Explantion of {elapsed_time} - El Forum - 10-26-2009

[eluser]Grind[/eluser]
Thanks. I now understand the elapsed time-bit. But is it possible to make a pseudo-variable for a guestbook? This would dramatically decrease the amount of PHP-code in the view-file.


Explantion of {elapsed_time} - El Forum - 10-26-2009

[eluser]rogierb[/eluser]
You can if you extend the output class.
But that might not be the best option, why not use a 'normal' variable to pass to the view?


Explantion of {elapsed_time} - El Forum - 10-26-2009

[eluser]Grind[/eluser]
Could it be achieved with one variable then? Now I have a foreach-loop in the view-file to display the guestbook, but that amount of PHP-code could be less? Where can I find how I do this? I'm still learning Codeigniter.


Explantion of {elapsed_time} - El Forum - 10-26-2009

[eluser]rogierb[/eluser]
Just cut the php code from your view, stick it in the controller (method) and put is in a variable. Then pass it to the view.

Code:
private function guestbook()
{
// do whatever you did in the view.
}

/* normal method */
$data['guestbook_var'] = $this->guestbook();
$this->load->view('my_view', $data)

/* view */
echo $guestbook_var;



Explantion of {elapsed_time} - El Forum - 10-26-2009

[eluser]Grind[/eluser]
Thanks for your reply. That's a lot better. But it doesn't work for a 100%.
My guestbook-function looks something like this:

Code:
private function guestbook()
{
    $this->db->order_by('id DESC');
    $query = $this->db->get('guestbook')
    foreach($query->result() as $row)
    {
        echo "<h3>".$row->name.'</h3>';
    }
}

But the guestbook is shown, even if the variable $guestbook isn't echoed in the view-file. Besides, the guestbook-function is echoed before loading the page. So if I look at the source I see all the guestbook-entries and after that the &lt;html&gt; and &lt;head&gt;-tags etc. So something is wrong.
I think I don't need to echo anything in the guestbook-function, but how does it work instead?

Thanks for your help.


Explantion of {elapsed_time} - El Forum - 10-26-2009

[eluser]rogierb[/eluser]
hi,

Don't echo in your controller, only in a view files. If you echo in you controller, you output before the headers and html.

Code:
$guestbook .= "<h3>".$row->name.'</h3>';

$guestbook = '';
foreach($query->result() as $row)
{
    $guestbook .= "<h3>".$row->name.'</h3>';
}

return $guestbook;



Explantion of {elapsed_time} - El Forum - 10-26-2009

[eluser]Grind[/eluser]
How obvious. Thank you very much!