Welcome Guest, Not a member yet? Register   Sign In
Generating HTML gradually
#1

[eluser]dobomode[/eluser]
It looks like the current architecture outputs the HTML at the very end of page execution when all the views have been loaded, etc.

I would like to build a page that runs long DB queries and provides feedback to the user as each query is completed. To do this, I'd need to be able to output HTML gradually in steps and not in bulk at the end. What's the best way to do this?

Thanks!
#2

[eluser]pistolPete[/eluser]
Is ajax an option?
Then you would periodically call a controller which returns the current state.
#3

[eluser]TheFuzzy0ne[/eluser]
I'm not so sure. I see no reason why you can't output a header with the start of your HTML page on (Doctype, html tags, head tags, title etc...), then loop in the controller with echo statements. I know it's naughty not to use views, but that will probably be the only way to do it without multiple requests. When you're done, you could output a footer, which would contain any code for the end of the page.

I suppose that it would be possible to have a view for display each row. As the output class will try to save your output until the end of the request, you can get around it by echoing the output as you call the view.

Code:
echo $this->load->view('header', NULL, TRUE); // The TRUE parameter flushes the object buffer, and returns the contents.
foreach ($whatever as $something)
{
    // Do something and echo the output with each iteration.
}
echo $this->load->view('footer');
The code above is untested.

Please see the section about loading views in documentation for the [url="http://ellislab.com/codeigniter/user-guide/libraries/loader.html"]Loader[/url] for more information.

I haven't tested this, but I see no reason why it shouldn't work.

Mortals beware, there be server timeouts here!
#4

[eluser]dobomode[/eluser]
Thanks guys.

I don't want to do AJAX cause the learning curve is steep.

I can echo without views, but I cannot mix echo and views as the suggestion above: if I try to echo in between view loads, the echo executes before the views get output (since views get processed and output at the very end), which basically makes my manual echo's appear before all view-related HTML output.

I guess what I need is a way to tell the view to output immediately and not at the very end. I could use the third boolean argument for loading the view to assign the HTML to a variable and then echo it manually, but I don't think that's very elegant...
#5

[eluser]TheFuzzy0ne[/eluser]
That's exactly what the code I've posted should to. That's why the third parameter has been set to TRUE.

I'm so confused.
#6

[eluser]pistolPete[/eluser]
[quote author="dobomode" date="1234757186"]
I don't want to do AJAX cause the learning curve is steep.
...[/quote]

Are you sure you don't want to try it?

Have a look e.g. at jQuery Heartbeat:
Quote:The jHeartbeat plugin allows you to create constantly updating pages by including 10 lines of code.
All you need to do is specify a page url, the time delay of the page refresh and off you go. It's as easy as that.

The plugin will retrieve the URL after the time delay has passed and refresh the data on screen using an ajax request.

Here are some other jQuery demos, which provide everyhting you need for your problem: http://www.ngcoders.com/pquery/demos/ajax/
#7

[eluser]dobomode[/eluser]
I'll gather the courage to explore AJAX one day. :-)
Thanks for the info.
#8

[eluser]dobomode[/eluser]
[quote author="TheFuzzy0ne" date="1234758057"]That's exactly what the code I've posted should to. That's why the third parameter has been set to TRUE.

I'm so confused.[/quote]

Oh ok, I skipped through that. I suppose that should work. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB