Jump to content

Dynamic Map creation


cooldude832

Recommended Posts

I have a script that retrive a map i've stored in a mysql table the data is like:

 

mapID X Y Type

1 1 1 1

2 1 2 1

3 1 3 1

4 1 4 1

5 1 5 1

6 2 1 1

7 2 2 1

8 2 3 1

9 2 4 1

10 2 5 1

11 3 1 1

12 3 2 1

13 3 3 1

14 3 4 1

15 3 5 1

16 4 1 1

17 4 2 1

18 4 3 1

19 4 4 1

20 4 5 5

21 5 1 1

22 5 2 1

23 5 3 1

24 5 4 1

25 5 5 1

 

so each type determines an image to show for that given grid square. 

my question is what do you think is the easiest way to extract the map data, sort it and then display a full map like

<html>
<table>
//Row x=1
<tr>
//Y=1
<td><Img src="$map[][$x].$map[][$y]"<td>
so to speak

I have this idea so far

<?php
echo "Displaying Map<br/>";
$result = mysql_query("SELECT * FROM map") or die(mysql_error());
$count = mysql_num_rows($result);
echo $count." Map Pieces found<br/>";
if($count>0){
while($row = mysql_fetch_array($result)){
$map[]['x'] = $row['X'];
$map[]['y'] = $row['Y'];
$map[]['type'] = $row['Type'];
}
?>

after i get it in the array i'm wondering what the best idea is?  Should i find the greatest value in x/y for the map and then do a while loop for that and simply sort by the Y row (how do i do that too, i'm sounding stupid right now) So then i echo out the grid squares (1,1)(2,1)(3,1) etc to Y max and if i hit Y make i echo out a </tr> and then if i hit Ymin i echo <tr> make sense?  any tips be great

Link to comment
https://forums.phpfreaks.com/topic/60325-dynamic-map-creation/
Share on other sites

If you know that there will always be 5 pieces of data for each line (that is, you will always have y co-ordinates 1-5 for each x co-ordinate), then i think this is fairly simple. If you sort your query by X then by Y you will get them in the order you need. When you loop through the data, you can then check to see if new rows are needed in your table dependant on the value of Y:

 

<table>
<?php
$sql = mysql_query("SELECT * FROM `map` SORT BY `x` ASC, `y` ASC") or die(mysql_error());

while($row = mysql_fetch_assoc($sql)){
if($row['y']==1){
	echo '<tr>';
}
echo '<td>'.$row['type'].'</td>';
if($row['y']==5){
	echo '</tr>';
}
}
?>
</table>

 

This might need to be more complex - it depends how your maps work.

Link to comment
https://forums.phpfreaks.com/topic/60325-dynamic-map-creation/#findComment-300175
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.