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
2010-03-28 12:16:32
Seems more viable than a many-to-many relation table (which @dario suggested).
strager
2010-03-28 12:23:43
@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
2010-03-28 12:58:35
@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
2010-03-28 14:36:59
@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
2010-03-28 14:42:07
+1
A:
You should store data like this
SQUARES(squareId, XYdataOfSqare)
USERSQUARES(userId, squareId)
dario
2010-03-28 12:20:14
+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
2010-03-28 12:50:09