Applying PHP to Content
If you have sections on your site, you have the option of putting similar pages into one PHP file to make it simpler to edit the content. Lets say you have 10 pages of content for 1 section, which we will name "Guides". Instead of making ten seperate pages for each guide, we can have only 1 page with the information with the 10 guides in it.
We start with the file guide.php, which should be similar from the one in the applying PHP to Layouts method 1. Now to input the info in this file we simply put to following:
<?
if(guidephp=="
pagename/url")
{
print ("
content here");
}
?>
Or for multiple pages of info:
<?
if(guidephp=="
pagename/url")
{
print ("
content here");
}
if(guidephp=="
pagename/url")
{
print ("
content here");
}
?>
This is a really simple code. Basically you put the name of page in the
pagename/url part. An example would be PHPGuide and then that becomes the url to the PHPGuide info:
http://www.pokezam.com/guides.php?action=PHPGuide
As you can see, I put
action=content'sname after the main file, which stores all of the information on guide.php. Now to add the content too, just put in it the
content here part. However, there are some rules to adding content in that part. In adding content or info for a certain you cannot use double quotation marks (" ") in between the already two quotation marks... And if you think about this according to English grammar it makes sense. So what do I do when i have
<img src="image.gif"> you might ask? Well there are three options you can do
<img src=image.gif> (without any quotations) or
<img src='image.gif'> with only one quotation or you can do
<img src=\"image.gif\"> (with a backslash before the double quotations). It's your choice, any one of these will work, but it must be done to everything that has a double quotation before it is put in the content part, otherwise you'll end up with tons of errors and the page won't work properly.
Now some people use page instead of action like
?page=PHPGuide, but if you do that the url would be
http://www.pokezam.com/guide.php?page=PHPGuide and code would be:
<?
if(=="
pagename/url")
{
print ("
content here");
}
?>
Remember, the choice is yours! If you are going to include left and right layouts, they can't have double quotation marks either or else the PHP in the menus won't work.
Handling forms with PHP
One of the most powerful features of PHP is its ability handle web forms. Why is it so powerful? Well it's because anything input by the user into the form is available for you to use. You can have the form print their name on the next page or you can have the form information emailed to you or the user; it's up to you. Below is an example of a form using PHP, which you can name affiliation.php:
<form action="mailinfo.php" method="post">
<input type="hidden" name="subject" value="Affiliation">
<b>Email Address</b><br />
<input type="text" name="email" size="20"><br />
<b>Site's Name</b><br />
<input type="text" name="sitename" size="20"><br />
<b>Site's URL</b><br />
<input type="text" name="siteurl" size="20" value="http://"><br />
<b>Button URL</b><br />
<input type="text" name="buttonurl" size="20" value="http://"> <br /><br />
<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value="Reset"></form>
Now the above form may just look like a regular form, but the difference is what happens to the data. By putting
action="mailinfo.php" in the form tag, it tells the form to transfer the data the user enters to mailinfo.php, which is where we start to utilize PHP to its full potential.
Below is mailinfo,php, which is where the data from the form that we submitted goes:
<?php
if ($email && strstr($email, "@") && $sitename && $siteurl && $siteurl != "" && $siteurl != "http://" && $buttonurl && $buttonurl != "" && $buttonurl != "http://") {
mail("webmaster@pokezam.com",
"Affiliation",
"E-mail address: $email\nWebsite: $sitename\nURL: $siteurl\nButton: $buttonurl",
"From: $email <$email>\r\n");
mail("$email",
"Affiliation at PokeZam.com",
"Thanks for applying to affiliate with PokeZam.com (
http://www.pokezam.com).\nYou will hear back soon on if you were accepted or not.\n\n-Alakazam",
"From: Alakazam <webmaster@pokezam.com>\r\n");
header("Location: http://www.pokezam.com/thankyou.php");
} else {
header("Location: http://www.pokezam.com/joinerror.php?e=$email&n=$sitename&u=$siteurl&b=$b uttonurl");
}
?>
I am now going to talk about the code above by lines. Line two says that if the email is filled in and contains a "@" and if all the other fields are filled and not left blank, to do whatever is between these brackets "{}".
Lines three through ten send an email with the information entered in the form to yourself and an automated response to the user who applied. Anything with a dollar sign ($) in front of it, such as $email is a variable. In affiliation.php, in the input tags, whatever you put in the name tags can be referred to as a variable by putting a dollar sign in front of it.
Line eleven sends the user to a thank you page after they have applied.
Line twelve and thirteen say that if the statement doesn't apply to the if statement on line two, then to send the user to an error page. Just be sure to take a close look at the URL. When it says
joinerror.php?e=$email&n=$sitename, joinerror.php is a page that you will create, while we declare a new variable called e, which stands for email and we use the & symbol to create more vairables.
Now why do we need an error page? What if some fields are left blank on the form, or it's filled out wrong, such as with a fake email without a @ symbol in it? Well, instead of receiving a blank form, the form will be rejected using the following code in joinerror.php:
<?php
if ($e == "" || !strstr($e, "@")) { echo "<li>Your email address appears to be invalid<br />"; }
if ($n == "") { echo "<li>You forgot to fill out your site's name<br />"; }
if ($u == "" || $u == "http://") { echo "<li>You forgot to fill out your site's URL<br />"; }
if ($b == "" || $b == "http://") { echo "<li>You forgot to fill out your button URL<br />"; } ?>
<form action="mailinfo.php" method="post">
<input type="hidden" name="subject" value="Affiliation">
<b>Email Address</b><br />
<input type="text" name="email" size="20"><br />
<b>Site's Name</b><br />
<input type="text" name="sitename" size="20"><br />
<b>Site's URL</b><br />
<input type="text" name="siteurl" size="20" value="http://"><br />
<b>Button URL</b><br />
<input type="text" name="buttonurl" size="20" value="http://"> <br /><br />
<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value="Reset"></form>
Now as you can see, lines two through five are if statements for if a field is left blank. After that, the same HTML form from affiliation.php is pasted for the user to fill out again. If information is missing again, the form will keep being rejected until everything is filled out correctly.
MySQL Database
What would PHP be without its partner in crime MySQL? MySQL is a database language, like Excel, where you have tables, with rows and columns to store data. PHP and MySQL work together in the following way: as with the example above, data is gathered through a web form using PHP and instead of just sending an email and losing the data, the data is stored in a MySQL database.
MySQL is a very complicated language, so I'm only going to go briefly into it. You can create a MySQL database by going to your control panel located at
http://www.yoursite.com/cpanel. Once you login, go to MySQL Database and create a new database. Once the database has been created, create a new user and add them to the database you just created with all permissions.
Now that your MySQL database is created, be sure to remember your database name, username and password. The database name and username have your cpanel username followed by _ and then whatever you called them; for example: pokezam_forums. The only other thing you need to know about MySQL, is that at the bottom of the MySQL page, then is a link to PHPMyAdmin, where you can select your MySQL database and click on the SQL tab and perform a MySQL dump, where you paste MySQL for a script you may be installing. You will never need to do that if you aren't a programmer.
I'm not going to go more in-depth into MySQL because I've told you basically everything you need to know to be able to fix any errors you may have. With MySQL you can create a member registration script where users enter their username, password and email; the information is stored using MySQL and then they can login to your site and have access to exclusive content. That is just an example of how you can use MySQL, but it's up to you.
Conclusion
This is just an introductory tutorial to help you learn the basics. PHP is a very powerful language and is changing everyday. With PHP you can create things like message boards, web polls and email forms. The best part about knowing PHP is that you'll be able to correct errors you get in your scripts; just put the error in Google and you'll more than likely find the solution because most mistakes are from forgetting to put a semicolon or quotation mark. Check out the Resources page in the Webmaster section for some links to sites with PHP scripts. Thanks for reading this guide!