Hey i'm trying to create a map for a game. I didn't know where to put this as this thread will handle HTML, CSS, JS, PHP, MYSQL and AJAX.

My experience, (just to show how much i can or can't do)
HTML and CSS : I know a lot about these languages.
PHP, MySql : I wrote the whole item database for my game, currently you can buy/sell items, put them in your backpack or stash, equip them, sort them by amount, name, type etc. this was a lot of repeating code and theres is still a lot to learn for me.
JS: i did a couple of tutorial and wrote a script to move an object around in a div.... thats pretty much it :D
AJAX: i just finished my first tutorial about how to work with JS, PHP and AJAX to sent and get a query from a DB.

So i have set up a testing database to run some tests on how to get this to work for me. The end result for this test should make the map rebuild with AJAX after the player moves a square. There is not much info about how to exactly do this with HTML, Javascript, PHP, mysql and AJAX so i'd like to open this thread in the hopes i am able to pull this off with some help. If i (we :D) pull this off i will post all the code needed for this small testing environment so everyone can learn from it. After that i can get on with my bigger project that needs a map like this. Off course if there is a comprehensive tutorial about how to do this i'd be glad to read that :D.

So i started making a query to get the tiles i need to draw in an array. Then with a foreach loop i can generate divs with the corresponding options and picture.

The current problem is, the query i got here just returns the tileID of the current position. I expect the query to give me 9 values (the current tile the player is on and all the tiles next to him (a 3x3 grid)). It's probably something very simple i just can't think of atm :D.

PHP Code:

$xpos = 3; //this variable should get feeded by a $_GET or $_POST or something.
$ypos = 3; //this variable should get feeded by a $_GET or $_POST or something.

I have build my table "world" like this:
systemID -> not really used yet as there is just 1 system.
orderID -> dunno if i really need this one as i could probably order my table from the xpos and ypos.
xpos
ypos
tileID -> refers to the primary key of the table where i store my tiles.

I have filled this table with 25 rows to represent a 5x5 grid. where all the outer tiles are 3 (water) the middle tile (3,3) is 2 (desert) and the rest are 1 (grass).

05-30-2010, 03:30 PM

madmenyo

I figured the query probably overwrites mapgen[tileID] therefor it just returns 1 value. If i do a num_rows it actually returns 9 so thats good.

Is there a mysql function that avoids rows being overwritten? or do i have to make a function that puts the data into a array?

05-30-2010, 05:12 PM

criterion9

Something like:

PHP Code:

while($row = mysql_fetch_assoc($result)){
//each row will be populated one by one
}

What you had before was only fetching the first row. As an alternative you could adjust your query to return a single row with the nine values populated.

05-31-2010, 01:53 AM

madmenyo

Yeah i should have looped trough my query to give each result a different ID.

Now how to get this array data into my HTML file to load the 9 tiles into my map container with AJAX? I'm hoping to learn this today. I'm not sure if i should somehow pass this PHP array to my html file and create a JS loop that generates the tile divs in my map container div OR make the loop in PHP and put that directly into the map container div in the HTML file.