Log in

View Full Version : Table Style Grid Map really sloooow



liamallan
12-15-2010, 11:58 AM
hey guys, i made a map using a table style method. but it is really slow to load! :mad:

here is the code:

<?php
//game map test

//set the grid size
$grid_y = (int)400;
$grid_x = (int)400;

//x and y rows to display at once
$display_rows = (int)199;

//default display cordinate
$x = (int)400;
$y = (int)0;

$param_x = $_REQUEST["xcord"];
$param_y = $_REQUEST["ycord"];

if (isset($param_x) && isset($param_y)) {
//validate that the parameter is a legit point
if (($param_x <= $grid_x) && ($param_x >= 1) && ($param_y <= $grid_y) &&($param_y >= 1)) {
$x = (int)$param_x;
$y = (int)$param_y;
}
}

//the grid position desired will be set to display in the center of the viewable grid
$display_half = round($display_rows / 2);
$other_half = $display_rows - $display_half;

//to display the target in the middle, you have to get the number of rows to display before and after it.
//some simple math to take care of that.
$start_x = ($x - $display_half) +1;
$end_x = $x + $other_half;

//if the $start_x variable is less than 1 the grid would be in the negatives. so set it to 1
if ($start_x < 600) {
$start_x = 600;
$end_x = 799;
} else
//if $end_x is off the grid we have to compensate and add the remaining rows to the start.
if ($end_x > $grid_x) {
$extra = $end_x - $grid_x;
$end_x = $grid_x;

$start_x = $start_x - $extra;
}

//same applies for the y axis
$start_y = ($y - $display_half) +1;
$end_y = $y + $other_half;

if ($start_y < 600) {
$start_y = 600;
$end_y = 799;
} else
if ($end_y > $grid_y) {
$extra = $end_y - $grid_y;
$end_y = $grid_y;

$start_y = $start_y - $extra;
}

//showing the current parameters

print "<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" class=\"cellback\" align=\"center\">";

//these 2 for loops represent the y and x axis
//using the data collected above the loops will properly display the grid
for ($Ty = $start_y; $Ty <= $end_y; $Ty++) {
//start new row
echo '<tr>';
for ($Tx = $start_x; $Tx <= $end_x; $Tx++) {
//show grid
DisplayGrid($Tx,$Ty);
}
echo '</tr>';
}

print "</table>";



function DisplayGrid($gridx,$gridy) {
global $x, $y;
$host="mysql4.freehostia.com";
$username="localhost";
$password="password";
$db_name="database";
$tbl_name="mapdata";

mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select db");
$query2 = mysql_query("SELECT * FROM `mapdata` WHERE x = '$gridx' AND y = '$gridy'") or die(mysql_error());
$rows2 = mysql_fetch_array($query2);
$id = $rows2['id'];
$status = $rows2['status'];
$bgcolor = null;
//highlight current select grid coordinate
if ($gridx == $rows2['x'] && $gridy == $rows2['y']) {

if ($rows2['type'] == '12') {
$cityinfo = 'onMouseOver="balloon.showTooltip(event,\'load:lorem'.$id.'\')"';
$stickinfo = 'onClick="balloon.showTooltip(event,\'load:lorem'.$id.'\')"';
$bgcolor = 'bgcolor="#FF0000"';
$style = 'style="border:solid 1px #000"';

print "<div id=\"lorem".$id."\" style=\"display:none\">";
print "<b><u>".$rows2['x'].", ".$rows2['y']."</u></b><br /><br />";
//print "<img src=\"../hostile.JPG\" alt=\"\"/><br />";
print "<b>Historical city</b> lvl".$rows2['type']."";
print "<br /><br /><font size=\"1\">click to make box stick</font>";
print "</div>";

echo "<td $bgcolor $cityinfo $stickinfo $style align=center valign=center width=\"4\" height=\"4\"></td>";
}
if ($rows2['type'] == '14') {
$cityinfo = 'onMouseOver="balloon.showTooltip(event,\'load:lorem'.$id.'\')"';
$stickinfo = 'onClick="balloon.showTooltip(event,\'load:lorem'.$id.'\')"';
$bgcolor = 'bgcolor="#00CC00"';
$style = 'style="border:solid 1px #000"';

print "<div id=\"lorem".$id."\" style=\"display:none\">";
print "<b><u>".$rows2['x'].", ".$rows2['y']."</u></b><br /><br />";
//print "<img src=\"../hostile.JPG\" alt=\"\"/><br />";
print "<b>Historical city</b> lvl".$rows2['type']."";
print "<br /><br /><font size=\"1\">click to make box stick</font>";
print "</div>";

echo "<td $bgcolor $cityinfo $stickinfo $style align=center valign=center width=\"4\" height=\"4\"></td>";
}
if ($rows2['type'] == '16') {
$cityinfo = 'onMouseOver="balloon.showTooltip(event,\'load:lorem'.$id.'\')"';
$stickinfo = 'onClick="balloon.showTooltip(event,\'load:lorem'.$id.'\')"';
$bgcolor = 'bgcolor="#0066FF"';
$style = 'style="border:solid 1px #000"';

print "<div id=\"lorem".$id."\" style=\"display:none\">";
print "<b><u>".$rows2['x'].", ".$rows2['y']."</u></b><br /><br />";
//print "<img src=\"../hostile.JPG\" alt=\"\"/><br />";
print "<b>Historical city</b> lvl".$rows2['type']."";
print "<br /><br /><font size=\"1\">click to make box stick</font>";
print "</div>";

echo "<td $bgcolor $cityinfo $stickinfo $style align=center valign=center width=\"4\" height=\"4\"></td>";
}

}else{
if ($gridx >= '600' && $gridx < '790' && $gridy < '658' || $gridx >= '600' && $gridx < '658' && $gridy < '800' || $gridx >= '728' && $gridx < '790' && $gridy < '725') {
$cityinfo = 'onMouseOver="balloon.showTooltip(event,\'load:lorem'.$gridx.''.$gridy.'\')"';


print "<div id=\"lorem".$gridx."".$gridy."\" style=\"display:none\">";
print "<b><u>".$gridx.", ".$gridy."</u></b><br /><br />";
//print "<img src=\"../hostile.JPG\" alt=\"\"/><br />";
print "<b>Drafting Range of lvl14</b>";
print "</div>";

echo "<td bgcolor=\"#AAFFAA\" $cityinfo align=center valign=center width=\"4\" height=\"4\"></td>";
}else{

echo "<td bgcolor=\"#FFCC00\" $cityinfo align=center valign=center width=\"4\" height=\"4\"></td>";
}
}
}
?>

here is a link to the map (http://shiftysplayground.co.cc/map.php)
your connection may timeout and say page isnt available because it takes so long to load.

is there any way i could use divs instead of a table? some pointers will be appreciated. :)