views:

89

answers:

4

I don't know if it is possible only with PHP but thought will ask here...

I do have a PHP code which updates data every 15 days. my problem is when it updates the entire process taking more than 5 seconds so user must wait until the updates done.

what I am looking is I want to do an background update while user is viewing the content. or after web page is loaded then show updating blinking text on it.

my current code is given below:-

$result = $db->sql_query("SELECT * FROM data1,data2,data3,data4 WHERE person='" .$name. "'");

$row = $db->sql_fetchrow($result);
$day = $row['regtime'];
$days = (strtotime(date("Y-m-d")) - strtotime($row['regtime'])) / (60 * 60 * 24);
if($row > 0 && $days < 15){

$row = ['name'];
$row = ['age'];
$row = ['place'];
$row = ['address'];
$row = ['lat'];
$row = ['long'];
$row = ['geo'];
//etc
}else{ //do update
//insert into database
}
A: 

mathew - i won't comment on the code per se, suffice to say that hopefully that's not contained within the page itself. anyway, with that out of the way :). php/linux (as far as i know) won't allow you to spawn new threads, so your best plan may be to invoke a server method via $ajax. that way, it's out of the hands of the page users scope and will keep their page responsive.

As i said, hopefully the code above is contained in some class that is accesible via a URL. then you simply invoke the method and return either an html result or some simple message into a status div.

jim
do you have anything that can be illustrated?? I dont mind if it is in ajax.
mathew
sure - give me 5 minutes and a seperate message below...
jim
+3  A: 

Solution 1: AJAX - something as described by jim

Solution 2: use flush() to send the output to the browser and your script will continue to make the update.

narcisradu
but how do I use flush() here??
mathew
narcisradu - +1 for the flush() option. tho of course care would have to be taken with IE on this front : 'Internet Explorer has an "optimisation" that makes it only render a page after it has received the first 256 bytes...'
jim
well then Ajax is the best option...but I am not at all familiar..I cant write a new code but will be able to modify it accordingly if some one can give a sample.
mathew
@mathew - place that piece of code with the update before calling the flush() function. The only problem is to update the page after the update. For AJAX you may use some library like jquery which is pretty well documented.@jim - I'm pretty sure it will send some html tags
narcisradu
+1  A: 

ok mathew,

the assumption here is that you've got an empty div on the page with an id=myStatusDiv. also, it assumes that you've got a url/php file /home/longprocess. add jquery to the page and then at the foot of the page, add the following:

<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            type: 'POST',
            data: "username=" + $("#username").val(),
            url: 'longprocess.php',
            beforeSend: function() {
                // you could also put a spinner img or whatever here too..
                $("#myStatusDiv").html("please wait, page being updated...");
            },
            success: function(result) {
                $("#myStatusDiv").html(result);
            }
        });
    });
</script>

basically, when the dom has loaded, the $.ajax function will silently run in the backgound. on completion, the myStatusDiv div will get populted with whatever status message you have generated from your longprocess method. this could be anything from a simple message to an 'almost' entire section on the page. your php page would only have to search for the named parameters sent via the data object - i.e. in the above example -> $name = $_POST['username'];

there are lots of examples on the net and here in SO.

good luck (hint: search google for 'php ajax jquery $_POST') ;)

jim

[edit] - of course, you'll have to include your $_POST variables in the $,ajax method... I've made an assumption that you've got a textbox input with an id=username. there again are plenty of examples, but here's a quick one i found to get you going: http://www.talkphp.com/vbarticles.php?do=article&amp;articleid=58&amp;title=simple-ajax-with-jquery

jim
Great Jim thanks. one question when I search will my result page shows old data till update is done or it says updating??
mathew
mathew - you can add an event to show the fact that the page is updating in the beforeSend: event. i'll update my example. also, if you do accept this as an answer, be sure to update your tags to include jquery so that future searches reveal your quest :)
jim
But Jim here is some problem I think...what if the data within 15 days and update isnt required??
mathew
mathew - in that case, your logic in the longprocess.php file should handle that. it would simply test for the last update against the current date and then just return an empty status message if no update is required - i.e. echo ''; this would be so quick as to be almost undetectable. does that make sense??
jim
+1  A: 

Instead of running the update on a request from a user you could also run a script every night through a cron job. This way the user is never bothered.

Neothor