Gw Temp

Menu

Tutorial - 'PHP Site #1- Index and Homepage' by Lucavi

An item about Programming Languages posted on

Blurb

A PhP tutorial on creating a simple index and homepage. A great PHP newbie tutorial!

Body

Hey this is Lucavi. The last tutorial I wrote was for rm2k3, but I decided to break off from it and show people how to use php in a simple manner. You can use Notepad to create php files, or even some big fancy program, though I prefer Macromedia's Dreamweaver MX. So, start up your editor and let's go through some php.

Before we start...
I would like to say that, yes, I know my code Isn't the best in world, but its easy to understand and it gets the job done. Oh, and I expect you to know basic HTML, because php alone cannot make a site (and this is a php tutorial, not html).

Using php

You can look back at all of you're .html/.htm files, and grab the code, because if you're gonna use php the way I do, you won't really need .html/.htm files any longer.
Create a file and save it as index.php. It would be handy to place it in a folder (I will refer to this as tut from hereon). Make sure all the files you create for this tutorial go to this folder. On this page, insert and introductionary image, such as "Enter here" with a cool picture in the background or something. It is fine to use HTML and no php at all when doing so!

Now, with you're image in place, give it a hyperlink that goes to yourdomain/tut/site.php?content=home

What this does is specify that the variable content in site.php will equal home.

Now create the file site.php. Again, using HTML, create a layout using tables, images, text... anything HTML is fine. For the page content, just write a load of QWERTY (nonsense). A simple layout would include a banner, nav links (Home, News, blah blah) to the left/right/top, and a big space for content in the middle.

Now, it is time for some real php. I will give the code and explain it.

Code:

$content = $_REQUEST['content'];
include"$content.php";
?>

You need to replace the QWERTY content with this code, as this code shows the page content.



The basic php tag. It tells the browser that php is in effect. Much like

$content = $REQUEST['content'];

This code is arguably un-needed, but it is more reliable to use it. Remember the ?content=home? Well, this grabs the value and stores it in $content. $ declares a variable within our page. In this case it is called $content. The name of a variable may be almost anything as long as it does not contain any spaces.
Next we use the equals operator. This sets the value of $content equal to the value of the next expression (what we are setting it to).
We set it to $_REQUEST['content']. $_REQUEST[''] gets a value from a variable within the current URL. So, we chose content, which equals "home". So, $content now equals "home".

There are many other functions for varibles. You can set them to numbers:

$num = 1;
$number = 7;
$maximum = 225;

You can set them to strings of characters:

$variable1 = 'Lucavi';
$name = 'Bob';

Or both:

$msg = 'L33T';
$gamingw = 'This is my 2nd tutorial';

Back to the point.

;

You may have noticed the semicolons (;) at the end of the php lines. This declares the end of the function (php command). Always use it. and if{} do not require it though. There maybe other functions that do not require the use of ;, but for now, just remember these 3 as an exception. ALWAYS USE ;!!

include"$content.php";

My favourite php command! This includes a file within the current page. For example, if include a file containing the words "I EAT FISH" on a blank page, "I EAT FISH" will appear. Also, if I include a file with text formatting before a bunch of text, it will appear formatted on the browser.

include""; tells php to include a file. We chose $content, which equals home, and then wrote .php. So the browser would read: include"home.php";. This is where our homepage content will be displayed. Remember that ;!

?>

This declares the end of php. Much like . As I said before, ?> needs no ;.

Now, you just need to create the file home.php and you have a basic layout using php, though you only have one page. In home.php, you can create whatever you wish, using HTML and/or php. Remember that it is your home page though.

Now, you will want to create another page. For an example, I will show you how to make a 'php info page'.

Before continuing, take the time to set your Home link to yourdomain/tut/site.php?content=home to let the user go back to the home page. Create a link in your layout (in the navigational links section?) that says "PHP Info". Make the link to the same page, adn change the ?content vale to info:
yourdomain/tut/site.php?content=info

Now, create a new php file and save it as info.php (you should be able to see why).

In the new file, using php, type the function:

phpinfo();

This function displays all the php configurations on the current host/server. Typing a category in the prenthesis (brackets) will tell php to look up only that category (for example phpinfo(INFO_VARIABLES);), but we want all the information for this tutorial.

You page should look like this:

phpinfo();
?>

With all your files uploaded, you can now access the homepage, and the php info page. The same layout (site.php) should be displayed, and the content should simply change to the relevant pages (home.php, info.php).

You can use this system to create as many pages as you want, without having to transfer/copy code for each page. All that is required, is to create a link with the ?content set to the filename (without the .php extension) and create your actual content.
It creates a similar effect to GamingW, where the layout stays the same and the middle content section changes.

Thank you for taking the time to read my tutorial. Laterz - Lucavi

Next part: #2- Random Quotes and more...