views:

112

answers:

3

I am making a 2D game in javascript/ajax, that will be using data stored in mysql database. Every user have got his own "area" made of small squares that can have some values. But I have no idea, how to store values of each square in mysql, when each user can have area with different width or height. Do you have some idea?

+4  A: 

How about storing the rectangle's four corners (x0, y0, x1, y1)? If you have an index on them, querying should be fairly efficient too, but your mileage may vary...

AKX
Seems more viable than a many-to-many relation table (which @dario suggested).
strager
@strager: I fail to see a many to many in @dario's solution. (maybe re deleted and resubmitted?) His idea is an extension of AKX's by adding a table for user data (thus N users to 1 square)
lexu
@lexu, @AKX's answer requires only a few fields in the user's table, no matter the size of the area. @dario's answer requires a `USERSQUARES` table with e.g. 25 entries for a 5x5 square.
strager
@strager: got it, thanks for the explanation, I completely failed to understand AKX's design .. I assumed storing all squares, not what he suggested, mea culpa!
lexu
+1  A: 

You should store data like this

SQUARES(squareId, XYdataOfSqare)
USERSQUARES(userId, squareId)
dario
+1  A: 

I would just save the coordinates, one entry for each square per user.

Table structure:

id 
square_x 
square_y
user_id

id is auto incrementing, square_x and square_y is for the square's position.

You can then insert every user's squares with single records.

Alex