Welcome Guest, Not a member yet? Register   Sign In
Run queries on shutdown
#1

[eluser]Freeze Dried Pop[/eluser]
Is it possible, or has anyone made an add on, that allows for the registering of queries to run on script shutdown?

For instance, when viewing an article I may want to add to the database that a specific user has read an article, increment the hit count for that article and mark a user being "online now".

That's three queries which don't need to be run in order to display the page, so i'd rather get it out to the user and then run it.

Now I realise I could do something like this (poorly written I know, its just an example):

Code:
class blog {
    function article()
    {
         $article = get_article(uri_segment(3));
         $this->load->view('article', $article);
         $this->db->query('INSERT INTO online_now VALUES ('.$session_id.', '.time().')');
         $this->db->query('INSERT INTO article_read VALUES ('.$user_id.', '.time().')');
         $this->db->query('UPDATE article SET article_hits = article_hits+1 WHERE article_id = '.$article->id);
    }
}

But this requires the controller doing all the work, i'd rather be able to set queries to run on shutdown from within library functions.

Is this possible or has anyone done anything similar? If not i'm sure I can knock something up myself but thought i'd ask anyway.
#2

[eluser]InsiteFX[/eluser]
PHP - register_shutdown_function

InsiteFX
#3

[eluser]Freeze Dried Pop[/eluser]
I'm well aware of the function and how it could be used to create something like want, like I said, just wondering if anyone had already done something similar.

I assume from your comment that Code Igniter doesn't already run a function on shutdown so using that function wouldn't overwrite it, although i'd be weary of the possibility of it being utilised in the future.

Then again it's not obvious at first glance where best to put such a feature, an extension of the CI controller would probably be best but then i'd still have to save the queries I want to run somewhere, again probably to the controller.

Is destructing the controller the last thing CI does?

If so I could probably do something as simple as, saving it all to $saved_queries in the controller and running through them in the __destruct() function of the controller.

Although i've got a bit lost, i'm sure I read somewhere that you can simply return the query string rather than running it, or is that not the case? Certainly I can't find that functionality in the docs when looking now.




Theme © iAndrew 2016 - Forum software by © MyBB