Interaction

Overview of the process.

Add an action after the transform:

"transform":{
    "rotate": { 
	"axis": { 
   	    "x": 0, 
	    "y": 0, 
	    "z": 1
	}, 
	"angle": 0, 
	"rel": true 
    }
},
"actions":[
    {
	"uri": "http://thoughtfulmonkey.com/layar-mmo/attack.php?id='.$userID.'&mon='.$results[$loop]['id'].'",
	"label": "Attack",
	"contentType": "application/vnd.layar.async"
    }
]

Simple target page:

<?php
 
$userID = $_GET['id'];
$monsterID = $_GET['mon'];
 
echo "<p>You are user with id: $userID</p>";
echo "<p>You are attacking monster: $monsterID</p>";
 
?>

The process:

Define a function that will subtract one value from another, but round negative numbers up to zero
Load the player's details from the database
Load the monster's details from the database

Allow the player to attack the monster (damage = attacker's strength - target's toughness)
Allow the monster to attack the player

If the monster has no health, then delete it from the database
Update the player's health - we won't do anything for zero health yet

Some serious code (divide into three: db loading, combat, db update):

<?php
 
function zeroCheck ($value){
	if ($value > 0) return $value;
	else return 0;
}
 
$userID = $_GET['id'];
$monsterID = $_GET['mon'];
 
echo "<p>You are user with id: $userID</p>";
echo "<p>You are attacking monster: $monsterID</p>";
 
include 'dbinfo.php';
 
 
// Find the player
$sql = $db->prepare( "SELECT * FROM players WHERE id=:id" );
$sql->bindParam( ':id', $userID, PDO::PARAM_INT);
$sql->execute();
 
// If any were found, then convert to array
$count = $sql->rowCount();
if ($count > 0) $userRes = $sql->fetchAll(PDO::FETCH_ASSOC);
 
 
// Find the monster
$sql = $db->prepare( "SELECT * FROM monster WHERE id=:id" );
$sql->bindParam( ':id', $monsterID, PDO::PARAM_INT);
$sql->execute();
 
// If any were found, then convert to array
$count = $sql->rowCount();
if ($count > 0) $monsterRes = $sql->fetchAll(PDO::FETCH_ASSOC);
 
 
// Simple attack mechanism: damage= strength - toughness
$monDamage = zeroCheck( $userRes[0]['st'] - $monsterRes[0]['t'] );
$monHealth = zeroCheck( $monsterRes[0]['hp'] - $monDamage );
 
$userDamage = zeroCheck( $monsterRes[0]['st'] - $userRes[0]['t'] );
$userHealth = zeroCheck( $userRes[0]['hp'] - $userDamage );
 
// Display results
echo "<p>You hit the monster for $monDamage damage.<br />";
echo "It now has $monHealth health</p>";
 
echo "<p>The monster hit you for $userDamage damage.<br />";
echo "You now have $userHealth health</p>";
 
 
// Update the database
if ($monHealth > 0){
	// Update monster health
	$sql = $db->prepare( "UPDATE monster SET hp=:newhp WHERE id=:id" );
	$sql->bindParam( ':newhp', $monHealth, PDO::PARAM_INT);
	$sql->bindParam( ':id', $monsterID, PDO::PARAM_INT);
	$sql->execute();
}
else {
	// Delete the monster
	$sql = $db->prepare( "DELETE FROM monster WHERE id=:id" );
	$sql->bindParam( ':id', $monsterID, PDO::PARAM_INT);
	$sql->execute();
}
 
 
// Update player health regardless
$sql = $db->prepare( "UPDATE players SET hp=:newhp WHERE id=:id" );
$sql->bindParam( ':newhp', $userHealth, PDO::PARAM_INT);
$sql->bindParam( ':id', $playerID, PDO::PARAM_INT);
$sql->execute();
 
echo "<p><a href='layar://mmodemo/?action=refresh'>RETURN</a></p>";
 
?>

Removing the need to visit another page. First remove all echo commands. Then add the following as the last line:

header('Location: layar://mmodemo/?action=refresh');