How To Develop Your Very Own PHP Game
Tutorial on making a PHP game, from scratch :)

Step 8: Some Basic MySQL

By justImagine
Okay, now that I've posted the way MySQL codes worked, I think we should move on to the codes.
Basically, I only use a few of the MySQL scripts as shown below:
1) INSERT
The way this script works is simple. Here's the syntax:
INSERT INTO('table_name') VALUES('data_to_be_added1', 'data_to_be_added2');
I hope you'd notice that when you use MySQL, it works where:
- The user logs in
- The first layer, the first group of gathered data, is the database
- The database contain tables
- One user can have several databases, and each database can contain several tables.

2) SELECT
This syntax is used to fetch data from a table.
SELECT column_name FROM table_name;
To select contents of all columns, the column_name field is filled with an asterisk (*) sign. This syntax can have an additional order, WHERE to select a specific row content.
With it, the syntax will be SELECT column_name FROM table_name WHERE='content_specification';.

3) UPDATE
This syntax is used to update a table content, may it be specific or all at once.
UPDATE table_name SET column_name='new_content', column_name2='new_content2';
This syntax could also use a WHERE addition.
UPDATE table_name SET column_name='new_content' WHERE='content_specification';

Those were some basic MySQL codes. To execute them, the PHP code is mysql_query($query,$mysql_id); where the $mysql_id variable is a fetchback result of the order mysql_connect(). The connection had a connection ID, and it's what the $mysql_id variable keeps.
 

Step 7: A Jumpstart

By justImagine
Oookay, I'm a little depressed here because there are so many things I need to talk about here and the lack of time I have. So, let's assume you've mastered several basic PHP Codes. You'll need the repetition statements (FOR, WHILE, DO-WHILE), conditional expressions (IF, IF-ELSE, IF-ELSEIF), and a basic like PRINT.

Now we're moving to MySQL database.

I kinda hope you can all understand MySQL. I must first warn you that MySQL systems are a bit different with PHP since MySQL works a a database. If you're about to make an MMORPG, you'll definitely need a database.
One good thing, is that PHP has the codes that integrates PHP codes with MySQL. Basically, when a player plays your game, the only thing he is doing is updating your database. Here's how it works:

A player registers >> When Register button is clicked, it wrote a new data entry in the database >> When account is verified, the data entered is updated into an Active Account >> Player logs in >> PHP Codes fetched the data based on the entered Username from database >> PHP Codes verified the input. If the Username and password match, then the result of the statement is TRUE. Else, it's FALSE >> Assume the result is TRUE, database updates the online-offline status of the player >> Player can start playing. Let's say the Player bought an item >> When the player verifies his buy, the Database will work on several things: 1) PHP Codes fetched the 'cash' the Player had. If it's enough to buy an item, no warning will be displayed. 2) PHP Codes calculates the remaining cash the player had. 3) The calculated remaining cash is re-written into the database. So now the database held the new cash data. 4) Data that a player bought the item is written into the database, so it won't be lost as the player lost his cash to buy it.

When a player asks for battle, here are the probable processes:
PHP Codes fetched the data available about players of the player's level >> PHP Codes displayed the results along with a link next to each shown username: Attack link. Making this can be done by using FOR or WHILE >> When the player decided to attack one other player, let's say Player2, PHP Codes fetched the attack points, defense points, and health points or anything else necessary from the Database. It calculates the fetched P2 data with the attacking P1's data. After the calculation, one will come out victorious. Let's say, P1 won. So he got the cash and Experience points. The way to add these is by fetching current cash and Exp, keeping them in one variable each, then using PHP to calculate the result of the current cash and Exp plus the gain. Then the current health minus the damage taken >> The calculations were kept in a variable each >> PHP is used to manipulate MySQL and update the current data with the results of the calculations.

If you could understand those, then you have almost enough to start learning MySQL. I'll talk about the scripts on the next post, alright?
 

Step 6: Conditional Expressions/IF() and IF()-ELSE()

By justImagine
The Expressions here doesn't refer to how your face looked in moods, but to a functional parameter in some PHP basic functions.
First, try making a file in your host and name it 'if1.php'. Then write these down:

<?php
$name = "Anton";

if($name == "Anton")
print("Welcome, Anton!");
?>


Those short script will have an output about like this:

Welcome, Anton!

Now, for the explanation. The term $name = "Anton"; creates a new variable called $name containing a string (a text or a line of characters that can be either alphabetic or numeric, and doesn't work as in for numeral processes like plus or multiply) "Anton". And then the function if() works as the conditional function. It states $name == "Anton" which pretty much means if the content of variable $name contains the string Anton. If the value of the test is true, then the statement print("Welcome, Anton!"); is executed. Keep in mind that it uses two equal signs (==). It means it does NOT create a variable, instead it checks the content of the variable whether it's the same as the conditional string (or content) or not. There is also three equals (===) to make difference between "1" with the boolean (true or false values, sometimes outputs 1 or 0) "TRUE" and "0" with "FALSE". There are also > (greater than), < (less than), >= (greater than-equal to), <= (less than-equal to), and != (not equal to). The first 4 can only be used in numerical operations, like if($number > 5) (the script means 'if the content of variable $number is greater than 5, but the statement to execute isn't written. Well, it's just an example, right?).
Now edit the if1.php and change the content of $name into "David", and add these after the print() on if():

else
print("Welcome, guest!");


The result will be:

Welcome, guest!

Why? Because we added the else() statement after if. This made the if() have the if()-else() form. The cause is, when the test on the if() returns FALSE value, then the statement on else() is executed. Like, if you're going to a mall, and you faced a two-way road. If you know that the road left side heads NOT to the mall, you'd go to the right side road. Like if(left_side_road heads to "mall"){ goToLeftSideRoad; }else{ goToRightSideRoad; } if you understand what I mean. In that case, I also used another form of if():

if(logical_test)
{
statement_if_true;
}


The underlined parts made it. Yup, it's if() with brackets. These are useful if the statement to execute under if() took more than one line. For example:

if(logical_test)
{
print("statement_1");
print("statement_2");

}


That if contains 2 statements to execute if the value is TRUE: the first print and the second one. Get it?

On the next meeting, we will talk about another conditional expressions. Keep your eyes peeled AND never get bored on learning!
 

Another Basic Step: Finding a Host

By justImagine
ANOTHER FORGOTTEN POST.
To start with PHP, you must at least install WAMP Server on your computer, or if you want the world to see your game, get a free hosting at the internet.
I'd recommend www.freewebhostingarea.com. It is free, it has short subdomains, and its file host is easy to use. To make/edit files, delete files, even with compressed files like those with *.zip or *.tar.
Another feature is that one email account can have multiple websites in a same server, like I have archaeozoic-area.6te.net and pals.6te.net on 6te.net server.
Not to mention it is trustworthy AND has a free 24/7 customer service. I'm not here to promote a brand, but this is the hosting I'd recommend you the most.
I think you'd better use 6te.net or ueuo.com because they have short subdomains and were not banned by FB (freetzi.com was reported abusive somewhy). To register, go to the server address (e.g. 6te.net) and add /create22.html behind the main link (e.g. ueuo.com/create22.html) in case the two servers doesn't appear in freewha.com combobox.
So, now with hosting, plot, and some basic programming, you can start training to make your own game!
 

Step 5: Basic Interface

By justImagine
This step includes the basic data processing thingy.
Now, make a new file and name it interface.php.
Write these down:

<form action="process.php" method="post">
<input type="text" name="name">
<br />
<input type="submit" value="Send">
</form>


The outcome is about like this:








Actually, the whole file was made of HTML codes, but it's fine to keep it with .php extension. The action="process.php" parameter sets the destination file when the 'Send' button is pressed. While the method="post" sets the method used to send the data input. Then the name="input" parameter on <input> tag names the inserted data as "input" for the superglobal $_POST. You can change it into "name" or anything else, but don't forget to change the name parameter in the box bracket on the new codes below.

Now make a new file, name it process.php, and type these codes in:

<?php
$name = $_POST["input"];
print("Hello, $name!");
?>


It's short, but when you input the name on the interface.php and pressed 'Send', it'll print:

Hello, *name_input*!

You can change the $_POST["input"] into $_POST["name"] or anything as long as it's the same with the text input's name, so the data won't be lost in the middle of the process.

For an instance, you enter "Chloe" on the text input and name the input "name", then the process.php must be changed first, change from $_POST["input"] into $_POST["name"].
OK, after it's changed, you can enter the name as you wish, like "Chloe" in our example now. Type it in, then press Send.
You will see the screen writes 'Hello, Chloe!'
The codes are short, but now you can have some interface basics. Now try this for another kind of experiment.
Open interface.php in editor mode, change the method into GET (method="get"). Then in the process.php, change the line $name = $_POST["input"] into $_GET["input"] (those examples showed the text input named as "input", if you name it with another name, be sure to change it as well).

When you enter a name,for instance "Anton", here's what you get in process.php:

Hello, Anton!

But look at the address bar. Taking example that the text input is named "input", here's what you get in the URL:

http://your_web_address/process.php?input=Anton

The underlined part shows you what the GET method does. See why it's called URL rewriting?

Now you should have some idea about processing data from form inputs. On the next meetings, we'll start with basic operations like if(),if()-else(),for(), and some more and see what they can do.
 

Step 4: PHP

By justImagine
Now time to start with PHP. First of all, I must warn you NOT to freak out if you faced an error, so don't be like me. Second, let's roll.
PHP is a kind of interactivity-creating browser-based language. This is what we'll use in the game making in this blog.
PHP codes are easy to notice. They start with <?php and ends with ?>. You'll also notice a lot of $ signs in the codes. The words after $ like $username is called variable, while the capital letters after $_ with parameters in box brackets like $_GET["name"] is called superglobal. The example variable we can call as 'username variable' while the superglobal is 'GET superglobal' (this superglobal had things to do with data sent through GET method from the form as explained previously on Step 2).

Here is an example of PHP code:
<?php
$name = "Alfi";
print("Welcome, " . $name . ".");
?>


Save it as php_try_1.php (note that the file format is .php).Those codes will create this outcome if you have a PHP supporting server or works with a file hosting that supports PHP:

Welcome, Alfi.

Dull, huh? Don't be mistaken, the print(""); function is pretty useful, we'll use pretty much of it later. Notice that the variable making ($username = "Alfi";) had "" marks to start and end characters (this differentiates characters like words and functions like print()) and ends with a ';' and so as the function. DON'T MISS THAT SIGN. Without it, the parser (the syntax checker on the server) would send an error message about Unexpected $end on *file_address* on line x.
Talking back to our first script, the print() functions works as an order to display characters on the monitor. For an instance, check our previous code:

print("Welcome, " . $name . ".");

It'll print the words 'Welcome, ' (with a space). Then the " sign after the space after the coma sign ends the words to be printed. Then there's a space and then a dot and another space, it works as a connector for an instant print through another function — in this case, through a variable. Then another connector, then the codes "." prints the '.' (dot) sign.
OK, maybe I explained a little too fast, so take a deep breath.
*Inhales deeply.*
*Exhales.*
OK, back to business.
The earlier functions prints the words 'Welcome, ' and then the content of the variable $name, and then a full stop mark.
Because we are printing a variable (and not a function), you could also make the codes written like this:

<?php
$name = "Alfi";
print("Welcome, $name.");
?>


It would have the same outcome. Now change the value of the $name into "David". It'll print this:

Welcome, David.

So, it's an example that a variable IS a variable — it can have various content. This is useful for game data keeps, and some interactions.

Try this too to see what PHP is capable of. Edit the source files of php_try_1.php and change the earlier codes into these:

<?php
$name = "Norbert";
print("Welcome, $name. Right now it's " . date("d M Y") . ".");
?>


It'll have an outcome like this (e.g. it was 6th of June, 2010):

Welcome, Norbert. Right now it's 06 Jun 2010.

Notice that the print function used connectors to print the date() function. Nice results eh? The date() function checks the time on server. While the "d M Y" indicates the format of the time writing. In this case, it uses a small 'd' which means date in two digits (06), a capital 'M' which means month in three letters format (Jun), and a capital 'Y' which means the current server year in four-digits format (2010). There will be a list for the indicators for this function later, but be patient because we had a lot to talk about — and the multiplayer version of our RPG requires MySQL database, which would get you some extra stuff to think about.

However, that's for now. In the next part, we'll talk about making the content of the variable through an interface with the HTML codes we knew and using some superglobals.
 

Step 3: Making Tables

By justImagine
Now, to tidy things up in your game, you'll need to make tables. You can start by the <table> tag. Then, you add one row with the <tr> sub-tag. Then you can start adding a cell by the <td> sub-tag within the <tr> and </tr> tags.
Here's an example code:
<table>
<tr>
<td>
Cell content 1
</td>
<td>
Cell content 2
</td>
</tr>
<tr>
<td>
Cell content below 1
</td>
<td>
Cell content below 2
</td>
</tr>
</table>


And the result is:











Cell content 1

Cell content 2

Cell content below 1

Cell content below 2


If you've got what the codes do, you can start making your own. Trust me, this is a piece of cake but it's pretty useful. It might came in handy sometimes!