<HTML>
<HEAD>
 <TITLE>New Document</TITLE>
</HEAD>
<BODY>
<?
  require 'at_dbconnect.php';
  //look for cookie
  $mymagnet=$_COOKIE['magnet'];
  $magnet_count=0;
  $magnet_id="";
  $magnet_x="";
  $magnet_y="";
  
  $ts=time();
  // Request all non-expired magnets
  $result = mysql_query(
                "SELECT ID FROM tblMagnets ".
                "WHERE UNIX_TIMESTAMP(Expires)>$ts ");

  if (!$result)
  {
      echo("<P>Error performing query: " .
           mysql_error() . "</P>");
      exit();
  }
  while ( $row = mysql_fetch_array($result) )
  {
      $magid = $row["ID"];
      //select last move for each magnet
      $moved=mysql_query(
                 "SELECT ID, Magnet, Xpos, Ypos, Symbol FROM tblMoves,(SELECT MAX(MovedAt) AS lastmove FROM tblMoves GROUP BY Magnet) AS dismags WHERE tblMoves.MovedAt = dismags.lastmove AND Magnet=$magid");
      if (!$moved)
      {
         echo("<P>Error performing query: " .
              mysql_error() . "</P>");
         exit();
      }
      
      while ( $row2 = mysql_fetch_array($moved) )
      {
         $magnet = $row2["Magnet"];
         $xpos   = $row2["Xpos"];
         $ypos   = $row2["Ypos"];
         $symbol   = $row2["Symbol"];
         if($magnet!=$mymagnet)
         {
             //echo("<P>$magnet - $xpos - $ypos - $symbol</P>");
             $magnet_count++;
             $magnet_id.$magnet.",";
             $magnet_x.$xpos.",";
             $magnet_y.$ypos.",";
             
         }
      }
   }
   echo("magnet_count=".$magnet_count."&magnet_id=".$magnet_id."&magnet_x=".$magnet_x."&magnet_y=".$magnet_y);
?>
</BODY>
</HTML>
