Hi, if you are viewing this tutorial I am hoping that you have looked over tutorial #1 as it is essential that it is done, otherwise I think you will be completely lost.
~SyringeX
What's all the advanced stuff your going to teach me?
Well, I don't think we're quite done with The Basics yet, and we won't be starting MySQL until either lesson 3 or lesson 4. In this tutorial, you'll be learning things that will help you maintain an easier web site.
What will help me maintain an easier website?
Well, first of all, your variables will, we already covered those, and will be working with them a bit more in this tutorial. We are also going to be looking over includes, one of the best commands in php.
Alright, open up notepad and get ready. Let's take a look at the simple
include statement. What the include statement does is include an external file from one of your folders. How would this help maintain an easier layout?
Well, let's say you had your navigation links table. You have about 50 pages, and you want to add a new link. You could add the link on each page, but that would be very tedious, this is where includes come in handy. By including a file named navigation.php that you've created, you can simply add the link in the navigation.php file, and all your pages with the included navigation.php page would then have that link! (navigation.php must be made manually, and should be constructed the same as your navigation table, without any other features)
Let's look at the include statement code:
<?php
$admin = "SyringeX";
echo '<h3>My Web Page...</h3>It is maintained by' . '$admin';
include ('navigation.php'); /*This includes the navigation file onto the page.*/
?>
Save it, open it up. Cool huh? If it didn't work, make sure you have a navigation.php file made with your nav table on it. You need to make sure it's in the same directory as the file with the include on it, otherwise you'll have to specify the folder it is inside. EX:
<?php include '/otherfolder/navigation.php'; ?>
Can I make everything else easier by creating a php layout?
YES. I highly recommend using includes. Things that often use includes are your navigation tables, tagboard and polls tables, headers and footers, and other optional information. Let's take a look at how we can construct this simple php layout.
<?php
include 'headerfile.php';
echo '
<table><tr><td valign="top">include 'navigation.php</td>
<td valign="top">inside this table is the main content</td><td valign="top">include ('extras.php');</td></tr></table>';
echo '
';
include 'footerfile.php';
Make sure when you do this that you have the separate files that you want created on here. That pretty much does it for the include statement, so let's take a look at a new one.
Now we are going to look at the IF statement
The IF statement is also a very important part of php. It reads to see if a part of a script is equal to, less than, or greater than something else. Depending on the answer, it releases different information. Let's look at the basic construct of an IF statement:
<?php
$admin = "SyringeX"; /*Our variable we're using */
if ($admin == "SyringeX") { /*The IF statement. Inside of the parentheses is what you are comparing, in this case, we are checking if the variable $admin is equal to SyringeX. If it is, everything after the bracer { (Make sure you have it!) will be what is displayed */
echo 'You have an awesome admin';
} else { /*Remember to use the closing bracer }. Now, Else is what happens if our variable is not equal to SyringeX. Say you changed the variable value to "dogfood", then everything after the Else bracer { would be displayed. */
echo 'This must not be a very good admin then :\';
}; /* Our closing bracer and our semicolon */
?>
So basically, if whatever is inside the parentheses are true (also known as 1) all information from the first bracer to the closing bracer will be submitted, otherwise everything after the else bracer to the ending bracer will be submitted. Try playing around with the IF statement a bit and see what you can come up with. If you've seen the generators I've created, a big part of these are using IF statements.
You can also check to see if two variables are equal to eachother:
<?php
$variable1 = "1";
$variable2 = "2";
if ($variable1 < /*Less than */ $variable2 { /*Checks to see if the first variable is less in value than variable 2. In this case, 1 is less than 2 */
echo '$variable1' . 'is less than' . '$variable2';
} else {
echo '$variable2' . 'is less than' . '$variable1';
};
?>
Cool isn't it? Now we are going to move on to the wonderful and delightful world of...
Functions!
Functions help save time, and a good ton of it. They allow you to store code that can be called by typing in the functions name followed by your parenthese ();.
Let's take a look at declaring, and then calling our function:
<?php
function sometext() { /*Just like the IF statement with your bracers, anything inside is what the function containts. Sometext is the name of this function. */
echo 'This is the text that will be displayed by our function';
}
/* Now the function is over, now it's time to call our function, or else the text won't be displayed */
sometext(); /*Simply type the name with the parentheses and you've called the function! Anywhere where you type sometext() is where the text specified will be.*/
?>
Pretty easy isn't it? Everything within the braces is the value of the function. It doesn't have to be just text, it can be variables with the your mysql user settings, cookies, sessions and so on. They come in handy for portal and game making. And to call it, all's you do is type the name of your function, followed by parentheses and semicolon, and you're done! This is pretty much it for lesson 2, soon we'll be hitting the advanced things, but look below for this lesson's Cool Script!
Cool Script!
This lesson's cool script is a hit counter. All's you have to do is create a file called hits.txt -- Insert the number 1 on it, and that is all. Then type in the following code, which will use a blend of some of the aspects we've talked about. It adds 1 everytime a page is opened to hits.txt, giving us a hit counter! Once you've created the actual hit counter file, include it by using the include statement (You ought to know how by now B) )
<?php
if(!$open = fopen("hits.txt", "r")) {
echo "Sorry, failed opening the file!";
}
if($read = fread($open, 1000)) {
$hits = $read + 1;
echo "Page hits: $hits";
} else {
echo "Sorry, unable to read file!";
}
if(!$open = fopen("hits.txt", "w")) {
echo "Sorry, failed opening the file!";
}
if(!$write = fwrite($open, "$hits")) {
echo "Sorry, failed writing to the file!";
if(!$close = fclose($open)) {
echo "Sorry, failed closing the file!";
}
?>
As for now, that concludes lesson 2. Lesson 3 will contain arrays, methods, and more!
Created by Syringex