PHP Programming > General PHP Help

Need help starting on a browser-based game

<< < (2/18) > >>

ShieldHeart:
PS the value 'UNISPAN' in my VB code is simply an integer value of 1000.

ErnieAlex:
Well, if you want to use 2D arrays this is done nearly the same way as VB.  The difference is the way you access them.
In VB it's array-name(dimension1, dimension2),   in PHP, it's array-name[dimension1][dimension2]...
And, for the random number, you use rand(min, max), like  TIron=rand(1,100)...

For X = 0 To UNISPAN
For Y = 0 To UNISPAN
intTIron(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTGold(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTFert(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTWood(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
intTSton(X, Y) = Int(((100 - 50 + 1) * Rnd()) + 50)
Next
Next
Would become something like this:

--- PHP Code: ---
<?PHP

for (\$x=1; \$x<=1000; \$x++);
for (\$y=1; \$y<=1000; \$y++);
TIron[x][y] = rand(1,100);
TGold[x][y] = rand(1,100);
TFert[x][y] = rand(1,100);
TWood[x][y] = rand(1,100);
TSton[x][y] = rand(1,100);
}
}

?>

--- End code ---

Note:  You do not have to set up the array, just start using it.  PHP will auto-type the values.  So, you may wish to set the values assigned in this loop as integers.  Use  intval(rand(1,100)) to make sure it is an integer.
Lastly, make sure you create these values OUTSIDE of any functions.  The creation of the arrays should be done in the main program not inside a function.  The reason being the "scope" of the array would not be available if created inside a function.   Variables and arrays created inside a function are gone when the function is done, unless they have been made global variables.
Hope that helps..  Good luck with your game!

ShieldHeart:
Thanks that does help me get the coding done for generating the values, however, I'm having trouble with the actual writing of the values into my MySQL database. Every time I generate a resource value for a tile, I need to write that value into my MySQL database. Is it possible to do this through PHP code? Do I need to do some sort of MySQL query through PHP in order to write the values into the database?

Since my database is so large (I have 1,000,000 tiles in total to enter values for) I simply can't populate the database by hand through PHPmyadmin or through MySQL code. I was wondering if it's possible to automate this through PHP instead. Thanks again.

Sabmin:
you would use something like:

\$query = "INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)";
mysql_query(\$query);

after initializing a connection to the database of course. You'd set this in the loop as when you establish the values it would insert them.

Hope thats what you were asking. :)

ErnieAlex:
ShieldHeart,
You didn't mention you were going to load it into a database.  I was hoping so... But, I answered you...

Suggestions, I would not load arrays as I mentioned in my previous note.  It would be a resource hog...
I would do the loop and just create the values directly in your SQL query.  Then, let the query store the values as Sabmin mentioned.   Also, it is very easy to read one location out of the database, simple and quick.

A couple quick questions:  The 1000x1000 map is going to be the only map used?  Each player will use it in general?  If so, that should be easy enough for you to handle.  Also, think about the format of the fields in your database.  Use INT's if possible as they will load quicker.

I am interested in seeing this project develope...  good luck with it...