Easy Ajax: A Case Study - Page 5

PHP


<? require('../includes/connect.php');

$interval=130; # Seconds

$entry_id=$_REQUEST['eid'];

$editor_id=$_REQUEST['aid'];

$checkq="SELECT workflow_created_by,workflow_created_on,

      NOW()-INTERVAL $interval SECOND,workflow_closed_on,author_name,NOW()

      FROM mt_workflow,mt_author WHERE workflow_created_by=author_id

      AND workflow_entry_id=$entry_id AND workflow_type=1";

$clearq="DELETE FROM mt_workflow WHERE workflow_entry_id=$entry_id

        AND workflow_type=1";

$reserveq="INSERT INTO mt_workflow VALUES (0,$entry_id,$editor_id,1,NULL,

      $editor_id,NOW(),NULL,NOW())";

$refreshq="UPDATE mt_workflow SET workflow_closed_on=NOW(),

      workflow_created_by=$editor_id WHERE workflow_entry_id=$entry_id

      AND workflow_type=1";

$rslt=mysql_fetch_row(mysql_query($checkq));

if($rslt[0]){

  if($rslt[0]==$editor_id){

    # Me!

    $rslt=mysql_query($refreshq) or die(mysql_error());

    echo 'OK';

    exit;

  }

if(Date("Y-m-d-H-i",strtotime($rslt[3]))>=Date("Y-m-d-H-i",strtotime($rslt[2]))){

    # Locked

    echo 'This article has been locked by '.$rslt[4].' since '.$rslt[1].'.<br/>

        (Server time is now '.$rslt[5].', and '.$rslt[4].

        ' was last seen at '.$rslt[3].'. Any contact within the last '.

        $interval.' seconds is considered current.)<br/>

        In order to ensure that editing changes are not lost, you will need to

        refresh this page in order to edit this article. And, of course, '.$rslt[4].

        ' will need to be done with it!';

  }else{

    # Old, so me!

    $rslt=mysql_query($clearq) or die(mysql_error());

    $rslt=mysql_query($reserveq) or die(mysql_error());

    echo 'OK';

  }

}else{

  # Nothing, so me!

  $rslt=mysql_query($reserveq) or die(mysql_error());

  echo 'OK';

}

?>

This is the code called as /mt/keepalive.php from the javascript. After ensuring that I can get to the mysql database and gathering my variable, I build a few queries, not knowing yet which ones I'll need. I then execute one, which is this:

SELECT workflow_created_by,workflow_created_on,NOW()-INTERVAL $interval SECOND,workflow_closed_on,author_name,NOW() FROM mt_workflow,mt_author WHERE workflow_created_by=author_id AND workflow_entry_id=$entry_id AND workflow_type=1

The table 'mt_workflow' is something I created, and tracks a couple of other things in addition to editing concurrency; hency workflow_type. In this case, I should end up with information about the last person seen editing the article, if any.

If there is no record matching this article ID, we take the outer loop, labeled "Nothing, so me!" There we reserve the slot for the user:

INSERT INTO mt_workflow VALUES (0,$entry_id,$editor_id,1,NULL,$editor_id,NOW(),NULL,NOW())

If there is a record, it might be the user's own from a previous call to this page, possibly 30 seconds earlier. If so, we take the first clause, labeled "Me!", and update the timestamp to note that we still have the article open:

Continued on the next page Page 1Page 2Page 3Page 4 — Page 5 — Page 6

Article tags

Spread the word
Bookmark and Share
Profile image for phillip-winn

Article Author: Phillip Winn

Phillip Winn is the Chief Geek for Blogcritics, and a blogger since 1995. He may currently be found and followed as @pwinn on Twitter.

Visit Phillip Winn's author pagePhillip Winn's Blog

Read comments on this article, and add some feedback of your own

Article comments

  • 1 - Aaman

    Dec 31, 2005 at 12:28 am

    I wish that Ajax book was cheaper:)

    Incidentally, is this true AJAX, does the reliance on PHP and SQL not mean a new paradigm, not reliant on XML?

  • 2 - Phillip Winn

    Dec 31, 2005 at 11:49 am

    It's still XMLHttpRequest, so all three elements of AJAX are present. The PHP and Mysql are, I think, incidental. It could as easily be anything else, or an external webservice, on the "back end."

Add your comment, speak your mind

Personal attacks are NOT allowed.
Please read our comment policy.
Please preview your comment.

blogcritics lists for Nov 26, 2009

fresh articles Most recent articles site-wide

fresh comments Most recent comments site-wide

most comments Most comments in 24hrs

top writers Most prolific Blogcritics for October

top commenters Most prolific Commenters in 24 hrs