Intro to PHP: Templating with PHP Functions lab

Key Words: PHP, HTML table element, function
 
See Wikipedia (http://www.wikipedia.org/) for a detailed description of the key words.

Preparation

You are required to have completed the objectives in the Iteration Lab before completing this lab.

Objectives

From this lab, you will learn how to create a simple PHP grid-based layout template that you can use to provide consistency on your website.

Create a PHP document with an HTML grid-based, table layout.

  1. Create a PHP document with the following layout.
     
       
     

    Using the following HTML code:
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr><td colspan="2"> </td></tr>
    <tr>
    <td width="30%"> </td>
    <td> </td>
    </tr>
    <tr>
    <tr><td colspan="2"> </td></tr>
    </tr>
    </table>

    Note: The top row (in red) will be our custom header function, the middle (in blue) will hold the content, and the bottom (in green) will be our footer

  2. Create the PHP code using this HTML layout.
    <?php
    // This function spits out the header row and the opening of the
    // content row - up to the second column. It takes a string parameter,
    // which print outs the <title> tag in the HEAD
    function myheader($ptitle){

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
      <?php echo "<title>" . $ptitle . "</title>; ?>
    </head>
    <body>
    <table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000000">
    <tr>
    <td colspan="2"><a href="/"><img src="./logo.gif" alt="My PHP Site" border="1"></a><hr size="1"></td>
    </tr>
    <!-- End Header and Begin Content Row -->
    <tr>
    <!-- Left Links Column -->
    <td width="170" valign="top">
    <table width="170" border="1" cellpadding="0" cellspacing="0">
    <tr>
    <td width="100" valign="top">Put Nav Links Here!</td>
    </tr>
    </table>
    </td>
    <!-- End Left Links Column -->
    <td valign="top">
    <?php
    } // Closes myheader()
    // This function spits out the closing of the content row
    // and the footer row
    function myfooter(){
    ?>
    </td>
    </tr>
    <!-- End Content and Begin Footer -->
    <tr>
    <td>This is my footer</td>
    <td> </td>
    <td width="130" nowrap>   </td>
    </tr>
    </table>
    </body>
    </html>
    <?php
    } // Closes function myfooter()
    ?>
  3. Now with out layout file coded in PHP, we can use it for the first time. Create a file called index.php and save it in the same directory along with your layout.php. Add the following code:
    <?php
    // include the layout file
    include './layout.php';

    //Use the my header function from layout.php
    myheader("Welcome to my website!");

    //Enter some content to the website
    echo "Here is some rather bland content.";

    //Use the footer function from layout.php
    myfooter();
    ?>
  4. PHP References

    W3 Schools
    http://www.webmonkey.com