1

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
}
hakre
  • 193,403
  • 52
  • 435
  • 836
mathew
  • 1,179
  • 2
  • 13
  • 24

3 Answers3

2

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

jim tollan
  • 22,305
  • 4
  • 49
  • 63
  • Great Jim thanks. one question when I search will my result page shows old data till update is done or it says updating?? – mathew Jul 15 '10 at 09:04
  • 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 tollan Jul 15 '10 at 09:08
  • But Jim here is some problem I think...what if the data within 15 days and update isnt required?? – mathew Jul 15 '10 at 09:28
  • 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 tollan Jul 15 '10 at 09:32
1

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
  • 753
  • 1
  • 5
  • 10
0

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 tollan
  • 22,305
  • 4
  • 49
  • 63