Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Problem loading view before running model
#1

[eluser]Xarren[/eluser]
Hi, I think I'm making a very basic mistake but I could not find the solution anywhere. Now my assumption was that all actions are done chronologically, yet in this case codeigniter has proven me wrong:

Code:
function resize_photos($id)
    {

        $data = array(
            'message'    =>    'Please wait until the resizing process is completed. Do not navigate away from this page until given a confirmation of success'
        );
        $this->load->view('message_view',$data);

        if($this->girls_model->resize_photos($id))
        {
            $data = array(
                'message'    =>    'Thumbs successfully created'
            );
            $this->load->view('message_view',$data);
        }
        
        $this->view_girls_list();
    }

This is taken from my controller. My intention is that it gives the first message asking the user to wait until the process is completed (it can take up to a couple of minutes, its resizing a lot of images - its for the admin section of the website), and then give a confirmation of success. What seems to happen right now is that it displays nothing, resizes the pictures, and then displays both of the messages. Any ideas on how to get it to do what I want?
#2

[eluser]Buso[/eluser]
Ajax. Can't be done with php alone
#3

[eluser]danmontgomery[/eluser]
[quote author="Buso" date="1276824075"]Ajax. Can't be done with php alone[/quote]

Just to expand on this, PHP doesn't send any output to the browser until the script has run to completion.
#4

[eluser]Xarren[/eluser]
So let me get this right. The link requesting resize_photos($id) in the controller is a javascript one, which first requests the message and then requests the resize function?

Not played around much with ajax before, appart of form validation, any good resource you could suggest for ajax based tutorials/interaction between ajax and codeigniter or php?

edit: is there no way to force php to flush its output?
#5

[eluser]Buso[/eluser]
I have this script that I use at home, that has no ending point, and that outputs some kind of log (it's a scrapper).. When I wrote it I didn't know about php not sending output untill the script finished, but for some reason it does after some time and the script keeps running. I don't know how it works, I guess that after a certain limit of output set to be displayed, it vomits it to the browser. I never digged into the subject, but I know it can happen

Anyway, what you wanna do is learn ajax. I don't know where you can learn it, but I strongly recommend that you use a framework (like jQuery for instance) from the beginning, and don't handle the XMLHttpRequest object (or whatever its name was) directly (as some old tutorials say -.-). jQuery makes your life easier

http://jquery.com/
#6

[eluser]cahva[/eluser]
Ok heres a real quickie using jquery. For simplicity I give you an example of a sleep app. When user pushes "Sleep for 5 seconds" button, it will send ajax call to take_a_nap() method which will return message when its done.

Heres the whole code and view.

sleep.php controller
Code:
<?php
class Sleep extends Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->view('sleep');
    }

    function take_a_nap($seconds)
    {
        sleep($seconds);
        echo 'Wakey wakey, rise and shine. Napped for '.$seconds.' seconds.';
    }
    
}

sleep view:
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
<$cript type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></$cript>
&lt;meta charset=utf-8 /&gt;
&lt;title&gt;Sleep&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    <button id="sleep" type="button">Sleep for 5 secs</button>
    <div id="status"></div>

    <$cript type="text/javascript">
        $(function() {
            $("#sleep").click(function(){
                $("#status").html("Please wait while sleeping").load("&lt;?php echo site_url('sleep/take_a_nap/5') ?&gt;");
            })
        });
    </$cript>
&lt;/body&gt;
&lt;/html&gt;

You can turn this example straight to use with your resizing. take_a_nap() method would be your resizing method, parameter $seconds would be the id. Just examine the code and you should get the idea.

EDIT: Argh.. The stupid parser removes javascript tags in code view. Just change the $ to s.
#7

[eluser]Xarren[/eluser]
Thank you, I really love these forums - my first question here and the replies were very helpfull and fast. I'm shocked, I thought I was wasting my time asking the question here - Cheers guys! I got it working (Well sort of, tweaking it now).




Theme © iAndrew 2016 - Forum software by © MyBB