This article covers general patterns and suggestions for further improvement. The code is certainly refactorable.
The strongest feature of PHP and MySQL is that it combines a server-side scripting language with a persistent data store. You can imagine the database as a global variable - and it might as well be, since it's as easily accessible as one. When to query the database is up to the programmer and his discipline.
Here is a general flowchart of a typical user case through the PHP files:
The two biggest parts of the grid project involve user interaction: logging in, and doing actions with the grid. Squares can be blackened to represent walls, and miniatures can be added or removed. The primary view is an HTML table that dispgrid.php spits out. Only minor adjustments are needed to accommodate adding or removing miniatures, and for editing squares:
Thanks to the database, I only have to worry about a small number of variables. That lets me collect and transfer data in two ways: through forms, and via session variables. I store database queries with an associative array, which is very useful in PHP: utterly convenient, as the code will show.
I control access to individual PHP files by testing whether a username session variable has been set. It is only set at the beginning with a successful login. In any other circumstance, kick the user back to the login page.
Session variable 'loggedin' is only set here if password matches the one from the database. |
Every PHP file checks for this session variable at the beginning, and boots the user out if it is not set. |
With PHP, I can generate dynamic content; in this case, an HTML table that populates relevant cells with information from the database. The foreach lets me iterate through the rows returned from a database query.
With forms, session variables and a persistent store, "pseudo" multi-user interfaces like this grid can be simulated. Every refresh of the view is a new hit on the database, pulling in the most recent data. This works because the grid depends on the turn-based mechanic of these kinds of games.
Hopefully this code helps get you started on the path to web apps, or to clear some assumptions. The best thing to do is to jump in! :)
Go forth... and conquer.
Further Investigation
AJAX for automatic refresh
SVG and <canvas>
jQuery drag-and-drop with pathfinding
Refined user interface
Colored squares
Randomized dungeons
Faster painting than checkboxes
Additional tables in the database
Zend, or object-oriented PHP
Security
Code
Reference
"PHP & MySQL for Dummies" [Amazon Affiliate Link]