PDA

View Full Version : Intermediate PHP & MySQL Tutorial I


Kaeliah
01-30-2011, 03:39 PM
PHP & MySQL have a ton of useful functions you can use in order to do what you want. In this tutorial, we'll look at creating simple games, and the functions you would need to create the game from scratch. Sounds a bit overwhelming, but don't worry, we'll go through it step by step. If you get lost or are confused about something, feel free to ask questions of me, or other coders on the forum. We don't bite, we nibble. Anyway, on to the game project!!


1. Planning & Outlining

If you've ever written an essay or done a craft you should know a little bit about planning and outlining. As a newer coder, it's crucial that you learn how to do this at least a little bit so you have an idea in your head before you get to the notepad. First start off with the challenge. What is it you want to do? In this case we're programming a card game. So let's outline the rules of the card game and how it should work when it's fully programmed.

Five cards are shown, each with the option of keeping or throwing out. The goal of the game is to get as high a poker hand as you can, and the better the hand the bigger the reward you receive.

Now how do you program something like this? This isn't a question I expect you to know the answer to just yet, as there are many aspects to programming this game that have not yet been covered by my tutorials, but try to think in programming terms. A good place to start is database needs. In this case, we should probably have a way to store each card and information about the card. You could also choose to store information about the rewards, but in this case we'll use a simple enough reward system you won't need to. Next think about what the program should look like and what functions it should have. Do you want guests to be able to play the game? Are there any possible ways of cheating? What should the game look like when it's done? Think about these kinds of things and write a simple outline for yourself like this:

Game Ideas
- Members only!
- Use forms so members can't cheat.
- Cards laid out with the options below each.

Once you think you've got everything covered move on to the next stage. Although don't worry, you can always add or take away from these lists, it's just a way to keep your brain organized.


2. Creating Tables

In MySQL you can get, update, create or delete information, which is what makes MySQL such a popular tool in PHP. Now it isn't required, but to make the program smaller and more dynamic we're going to store all the card information in a table. But before we get into creating a table, we need to go over some vocabulary.

Table - Think HTML table. That's what it looks like. It stores information, and also information ABOUT the stored information.
Field - An equivalent in HTML would be a column of a table. A field is simply one column of the table that stores one type of information.
Null - Null basically means empty. Setting a field type to 'Null' allows it to be empty.
Primary Key - A primary key makes each row in the table unique. Most often it's used as an 'ID' field.
int - Is short for 'integer' and indicates the field holds number values only.
varchar - Holds any characters.
Auto_Increment - Automatically sets a value and increments it every time a new row is made.

Don't worry, you'll get to see each of these vocab words in action. Now on to creating the table! Start a new file and call it 'install.php'. Do not run this file until the tutorial tells you to! Now write in the start and ending for a php file(Refresher: '<?php' and '?>') and then we'll start the MySQL statement to create the table as follows...


/* This is not part of creating the MySQL table, you need this to connect TO the database first!
Make sure you have a copy of the Mysidia Adoptables already installed,
or you can set the variables to a separate database if you'd like */
include("inc/config.php");
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to MySQL');
mysql_select_db($dbname);
// End connecting

// Start Creating
mysql_query("CREATE TABLE cards
(
CardID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(CardID),
CardValue int(11),
CardSymbol varchar(120),
)
");


Now if you're confused, don't be worried. 'CREATE TABLE cards' is telling the database to create a new table, and call it cards. Then within the inner set of '( )' we're listing fields. The first field is CardID, it's information will be numbers or 'int', it cannot be empty or NULL, and it will automatically increment with each row. Because the AUTO_INCREMENT will make 1 value in each row completely unique, we'll make it the primary key as seen in the next line. Then after that we create a second field called 'CardValue' which will be a number(Jack = 11, Queen = 12, King = 13, Ace = 1). 11 is the standard integer length, meaning it allows a sequence total of 11 numbers. The third and final field is the CardSymbol, wheter it's hearts, diamond, spades or clubs.


--- Work in progress ---

Hall of Famer
01-30-2011, 10:41 PM
This looks like a very interesting Kaeliah, it will help a lot for programmers with a certain degree of PHP skills.

Kaeliah
01-31-2011, 10:57 AM
It's a work in progress right now. ^.^ More to come very soon.

PTGigi
01-31-2011, 02:01 PM
Looks nice so far :usedusedused:

LOL I fail at the first step XD I never do that :smile: Well only because I usually do that step in my mind for about a week or more and get the idea stuck there XD

Hall of Famer
01-31-2011, 03:20 PM
Well I was wondering if there's a way to store a double type variable in mysql, looks like the only way to do that atm is to store it as string, and then convert to double when calling from PHP.