Question : Create a map in PHP

I want to be able to make a table that puts the userxpos and userypos in the center of a 5x5 grid (using table's), then if the player presses an up button, the userypos becomes 4 but stays in the center of the grid, the map then moves. Please see code attachment

On object could appear on any of the boxes, which scrolls with the players interaction.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
P = Player at position X=5,Y=5

    3   4   5   6   7
  +---+---+---+---+---+
3 |   |   |   |   |   |
  +---+---+---+---+---+
4 |   |   |   |   |   |
  +---+---+---+---+---+
5 |   |   | P |   |   |
  +---+---+---+---+---+
6 |   |   |   |   |   |
  +---+---+---+---+---+
7 |   |   |   |   |   |
  +---+---+---+---+---+

If player presses UP then then the map will be as follows

P = Player at position X=5,Y=4
O = Object at X=3,Y=2

    3   4   5   6   7
  +---+---+---+---+---+
2 | O |   |   |   |   |
  +---+---+---+---+---+
3 |   |   |   |   |   |
  +---+---+---+---+---+
4 |   |   | P |   |   |
  +---+---+---+---+---+
5 |   |   |   |   |   |
  +---+---+---+---+---+
6 |   |   |   |   |   |
  +---+---+---+---+---+

Now as you can see there is an object at X=3,Y=2. 

As well as drawing the map, How would that object be pulled from a database? baring in mind that multiple items could be shown on the map at once. 

Any help appreciated.

Answer : Create a map in PHP

You will have to find a way to represent objects and their locations.
One way would be to have a table of objects, along with their coordinates:

tblLocations
Name, Symbol, X, Y

When drawing your grid, you can then get the list of objects at any location with a query:

select Symbol from tblLocations where X=3 and Y=2

The query will return a list of objects to draw at that location.

The PHP code can construst these queries based on the central position:

$myposx = 5;
$myposy = 4;
$gridwidth = 5;
$gridheight = 5;
$left = myposx - floor($gridwidth/2);
$top = myposy - floor($gridheight/2);

echo '<table>';
for($y = $top; $y <= $top+$gridheight; $y++)
{
  echo '<tr>';
  for($x = $left; $x <= $left+$gridheight; $x++)
  {
    echo '<td>';
    $result = mysql_query("select Symbol from tblLocations where X=$x and Y=$y");
    while($record = mysql_fetch_assoc($result))
    {
      echo $record['Symbol'];
    }
    echo '</td>;
  }
  echo '</tr>';
}
echo '</table>';

This code has not been tested, it is just to give you an idea.
Random Solutions  
 
programming4us programming4us