开发者

Background update via PHP?

开发者 https://www.devze.com 2023-01-07 01:31 出处:网络
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 s

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
}


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&articleid=58&title=simple-ajax-with-jquery


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消