Category: require command

02/06/08

Hello World - Use the include command to get you addicted to PHP

Permalink 05:44:08 pm, Categories: PHP, include command, require command  

Hello World!

After understanding PHP a little better, printing out Hello World is actually not the best to get a person hooked.

The way I got hooked to PHP was the discovery of the “include” command.

In 1999, I made a single-page website. I thought it was enough to announce my projects. Then I realized it would be better to place the contents, which were divided by subheadings, into separate pages.

So I had to make an initial template, with a single html table, with a top row for the logo and horizontal navigation bar, a left-side column vertical navigation bar, and a bottom row copyright line.

I finalized the contents that would repeat (the logo, the horizontal and vertical navigation bars, and the copyright message) because I was going to copy the format and paste them to all the pages I wanted to have.

So, I cut and pasted the template on separate pages, and placed each page’s content in the middle box of every page.

Guess what? It occurred to me that if I decided to eventually have 150 pages, I would have to go back to each and every page that I had already created, just to make a single change in, for example, a link on a navigation bar!

Once you get hooked on making websites, you will end up with a lot more pages (and ideas) than what you initially planned.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

With the include command, you can initially make, for example four files. One file would just contain the top portion of the webpage - name it “top.htm". The second file would only contain the left side navigation or left column - name it “left.htm". The third would have the copyright line - name it “bottom.htm".

Then you make the fourth page - an “example” page, which is a single page, with the unique contents of that page, AND the top, left and bottom contents. You can do CSS, if you know how, but for now you will just be designing the page using tables. So the table would have the top row, where the logo and top horizontal navigation bar would be, the left side column for the navigation bar, and the bottom row for the copyright line.

What you do next, is to highlight the top portion on that fourth page, delete the logo and the navigation bar, and right where the cursor is blinking in your dreamweaver or whatever else you are using, type in “xxxxx".

Then look at the page’s source code, look for “xxxxx” and in its place, type in

Code:

<?php
include ("link/here/top.htm");
?>

You can actually replace “link/here/top.htm” with something like “http://yourdomain.com/folder_if_any/top.htm". There are a few ways to do this.

Go back to design view, highlight the left side navigation bar, and replace it with “xxxxx". Then go back to the source code view, and look for “xxxxx". Replace this with “http://yourdomain.com/folder_if_any/left.htm".

Do the same with the copyright line.

WAIT!

Before you make a second and third page for your website, you must test your files for this first page!

Upload the pages to your site, open a browser and type in the URL for that fourth page.

You must see your page as complete, with all the four elements intact! If not, the links to the supplementary files would need to be changed. This is where you experiment further. Sometimes, just using “../folder_if_any/top.htm” would work. Sometimes, you may have to use just one period or none at all - try “./folder_if_any/top.htm” or “/folder_if_any/top.htm” or “public_html/yourdomainfolder/folder_if_any/top.htm” or something else.

Everytime you replace the links as shown above, you need to refresh the page on your browser. Once you see all the elements intact and included on the page, you have succeeded!

You can now proceed to make the first page your template. For the second page, copy all and paste the contents of the first page on to a second new page, and right where the section needs for the content to change, erase the previous content and paste (or type) the second page’s unique content. Upload to the same location as the first page. Test the second page… you should continue to see the top, left and bottom contents.

Note: When you upload to another folder, you might encounter an error, which might say that your include commands did not find the necessary files. This is when you start experimenting again with your links.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

What’s the use for all this?

Christmas! When the season changes, you might want your top logo to change, like you might want a snowman next to your logo.

Let’s say you started your website in July, with 5 pages. By December, you have come up with 50 pages. You must anticipate progress. What if you ended up with 500 pages?

With the include command, you simply go to that single top.htm page, change the logo into a logo with the snowman, and save.

Go back to your browser and check 10 pages… all ten would have the logos updated! You have 500 pages? You can be sure that all of them have been updated, and your website’s visitors would see the new logo in all the pages.

Let’s say you started with 5 links on the left side. Then, the following week, you realized that by including another page of useful information would enhance your website. So you have to add a single link to the left navigation bar. Once again, all you need to change is the left.htm page. All the pages in your website will change.

So that’s what “include” is useful for! It saves a lot of time, you don’t have to go back to each and every page, just to change a small portion!

Val Zubiri

Programming by Val Zubiri
Art by Val Zubiri
Arts Global Impact.org / Original Art and Art Books for Collectors for sale
Chicago Art Movement.com
Get a Massage Contact and Shop using your mobile phone

September 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

PHP and MySQL

Tips on the use of PHP and MySQL for your website. If you need help with your site, ask and I'll see if I can help. If you need long-term help with your website, let me know as well.

Search

XML Feeds

What is this?

powered by b2evolution free blog software