cooldude832 Posted July 17, 2007 Share Posted July 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 17, 2007 Share Posted July 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.