Lila Avenue

Website Projects

Build a CMS Step 6

If you have made it this far, congratulations!

On to the next task. You may have noticed that the code for our 3 pages is nearly the same. They all start out with a header, then some HTML blocks, and end with a footer. We could use this formula in index.php if we knew which page had been selected.

Formula for creating a page

First, let's have the page files just show the blocks that are required - we will change them into .txt files showing the required blocks separated by commas.

Contents of about.txt:  Contents of the about page file

We have added some column information to the 'blocks' file so that columns can be selected as well as blocks:

Contents of content.txt:  Contents of the contact page file

Next, let's change the menu so that index.php is the destination of each page, with 'query strings' to indicate a particular page. The About page can be requested by adding '?page=about' to the end of index.php.

Menu using queries

So, how does index.php use the query string to generate a page?

Warning - more PHP!!!

Reading the query string

if the About page was selected, index.php should echo the correct blocks.

Reading the query string

Warning Warning, Danger, Danger - even more PHP!!!

There is one more bit of PHP needed so that index.php can do its job. We are going to create an array with our string of blocks. A array is a collection of names that PHP knows how to use. For the 'about' page, the array would have 2 values - 'about.htm;' and 'sale.html'.

index.php can now find the right text file, using the query string, and the 'explode' command will turn the contents of this file into a array. (PHP convention: names can be made up of variables or plain text. Text must be enclosed in quotes and everything is connected together with periods.)

php explode

That's great, you say. So, there's a array for the about page. Now what?

PHP has a very useful command called 'foreach' that works just like it sounds. ('Trim' removes extra spaces.)

PHP foreach command

Whew!! That was a lot! These basic commands will allow you to add other pages and content to your website. Since each page is generated by index.php, it's considered a 'dynamic' website.

Step 7 will use just a little bit more PHP to set up an administrative page... stay tuned!

CMS-06 file structure

Download the code


Blocks

Inc

Pages

Our three pages are still here!

2 college cats CLICK HERE FOR DEMO