Sign in to follow this  
PHPCoder

[web] Dynamic Map Help

Recommended Posts

I'm trying to display a small 9 x 9 map here. The script sends a query, grabs any rows that are 4 + / - of the user's x and y coordinates. It then displays a map, and uses the coordinates to accurately place the users found onto the map. Here's the need-to-know part of my database table setup: kow_user_tbl ------------------------ intX, intY, username, gold, level, race, fortification_type There is only one row inside this table for testing purposes: intX = 1, intY = 1, username = Admin, gold = 4000, level= 1, race = Human, fortification_type = woodenencampment Here is a slightly edited version of the script (read the comments to understand the script, it is heavily commented):
Quote:
<?php //For testing purposes $username = 'Admin'; $conn = mysql_connect ("localhost", "dbuser", "dbpassword"); mysql_select_db("kow_user_tbl"); //SQL, Query, and Array to get the script started... $sql = "SELECT * FROM kow_user_tbl WHERE username = '$username'"; $query = mysql_query($sql); $begin_array = mysql_fetch_assoc($query); $mid_x = $begin_array['intX']; //Admin's X Coordinate $mid_x = $begin_array['intY']; //Admin's Y Coordinate $min_x = $mid_x - 4; //Minimum x Coordinate $max_x = $mid_x + 4; //Maximum X Coordinate $min_y = $mid_y - 4; //Minimum Y Coordinate $max_y = $mid_y + 4; //Maximum Y Coordinate /* Selects all users from kow_user_tbl that are within Admin's x coordinate (4 above or 4 below) and Admin's y coordinate (4 above or 4 below) */ $query2 = mysql_query("SELECT intX, intY, username, gold, level, race, fortification_type FROM kow_user_tbl WHERE intX >= $min_x AND intX <= $max_x AND intY >= $min_y AND intY <= $max_y"); //Blank array to store the players grabbed by query $players = array(); //for the while loop $i = 1; /* Sort all the players grabbed by query2 into the array 'players' by listing the information from the mysql_fetch_array'd query2 */ while (list ($intX, $intY, $user, $gold, $level, $race, $fortification_type) = mysql_fetch_array($query2)) { $players[$i]['x'] = $intX; //X Coordinate $players[$i]['y'] = $intY; //Y Coordinate $players[$i]['username'] = $user; //Username $players[$i]['gold'] = $gold; //Gold $players[$i]['level'] = $level; //Level $players[$i]['race'] = $race; //Race $players[$i]['fortification_type'] = $fortification_type; //Fortification //Increment i to continue player info in array $i++; } /* Every user has 1 of these (5 in total) fortifications. Depending on which fortification the user has, a specific tile is displayed. This function takes the fortification of the user, selects a tile, and displays it with an anchor around it so that it can be clicked for further use. */ function display_tile($user) { $tile_query = mysql_query("SELECT * FROM kow_user_tbl WHERE username = '$user'"); $tile_array = mysql_fetch_assoc($tile_query); $tile_fortification = $tile_array[fortification_type]; switch ($tile_fortification) { //Wooden Encampment case 'woodenencampment': $img_src = 'tiles/woodenencampment.png'; break; //Moat case 'moat': $img_src = 'tiles/moat.png'; break; //Fortress case 'fortress': $img_src = 'tiles/fort.png'; break; //Castle case 'castle': $img_src = 'tiles/castle.png'; break; //Palace case 'castle': $img_src = 'tiles/palace.png'; break; } echo "<a" . "\n"; echo "href='setattack.php?user=$user'" . "\n"; echo "><img" . "\n"; echo "src='$img_src'" . "\n"; echo ">"; } /* This series of loops displays the map. It initially sets y as the minimum a y coordinate can be (Admin's y coordinate minus 4). Y can be as high as the maximum y coordinate (Admin's y coordinate plus 4). Then another for loop is set for x. $pop is set to 0. If it remains 0 by the time it gets to the end of the second for loop, a default image is output (tiles/grass.png), which is the grass tile. As you can see, there is an if statement inside a third for loop. The third for loop is just there to increment $a so that it cycles through the players array. The if statement is there to check whether the current player's (the current players is chosen by $a) x and y coordinates match with the current $x coordinate and $y coordinate. If it does, it initiates the display_tile function so that it can properly choose the correct tile, and then display it. It then sets $pop to 1 so that it can break out of the second if statement. */ for ($y = $min_y; $y <= $max_y; $y++) { for ($x = $min_x; $x <= $max_x; $x++) { $pop = 0; for ( $a = 0; $a < count ( $players ); $a++ ) { if ( $players[$a]['x'] == $x && $players[$a]['y'] == $y ) { $user = $players[$i]['username']; display_tile($user); $pop = 1; } } if ( $pop == 0 ) { echo "<img" . "\n"; echo "src='tiles/grass.png'" . "\n"; echo "/>"; } $pop = 0; } echo "<br />" . "\n"; } ?>
The problem is, is that I don't think the selected rows are being properly stored into the array. When I test it on my server, something goes through the first if statement in the series of for loops while x and y are at 0, and a is at 0. This means that $players[0]['x'] and $players[0]['y'] were equal to 0. But the while loop that I made to store the players into the array uses $i, and $i is originally sets to 1, and from then on only increments, so it is impossible (from what i know...) for $players[0] to exist, since the minimum is $players[1]. On top of that, the only row inside the database has its coordinates at 1, 1. Does anyone see the problem?

Share this post


Link to post
Share on other sites
I changed it to that, and the numbers matched correctly.

But for some reason the function is still returning this HTML code:

Quote:

<a
href='setattack.php?user='
><img
src=''
><img


The problem is in the a href (user isn't filled in) and in the img src (the source URL isn't filled in), which is probably being caused by the value of $user, since when I try to output $user it doesn't output anything.

[Edited by - PHPCoder on October 20, 2006 4:04:08 PM]

Share this post


Link to post
Share on other sites
kind of offtopic: how far are you with this game?
i have tried something like this once with php and sql, i wrote it badly in the begining [just for testing, no tables, no aspect, just text] and it eventually went a bit far, and i quit.

may i see a how far you are? (i.e - may i have a test account [smile] ?)

thanks,
izua

Share this post


Link to post
Share on other sites
Try doing a print_r or var_dump on the $players array. That'll allow you to see exactly what data is in the array and make sure everything is working as you expect.

Share this post


Link to post
Share on other sites
I tried print_r.

Here's the output (with some line breaks)

Quote:

Array (
[1] => Array (
[x] => 1
[y] => 1
[username] => Admin
[gold] => 4500
[level] => 1
[race] => Human
[fortification_type] => woodenencampment
)
)


Everything looks good.

And the game is nearly done, this is the second to last thing I need to finish. I'll PM you a link to the game once it is finished. :)

Share this post


Link to post
Share on other sites
I woke up this morning, and not a second later than when I woke up the answer to the problem suddenly came to me, and I wasn't even thinking about the code at the time, lol.

It's a silly mistake

Change $user = $players[$i]['username']; to $user = $players[$a]['username'];

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this