0

Writing a New Class

I have been writing and testing a new PHP class today to make me easier to create my new site menu bar called menu class. It contains a nested unordered list and a for loop to create the sub-menus. I wrote a couple of variables that use explode function to convert the input values to arrays. Here is the code I have written so far:

<?php
class menu {
    function  __construct() {
            // Output the menu
        echo "<div id='nav'>n";
        echo "<ul>n";

        $this->item("Home", "/");

        $this->item("Blog", "blog");

        $this->item("Portfolio", "portfolio",
                "Resume=>resume,
                 About Me=>about");

        $this->item("Projects", "projects",
                "ICAP<br />Interagency Coalition on Adolescent Pregnancy=>icap");

        $this->item("School Assignments", "assignments",
                "CA 272<br />Pro Website Dev=>ca272,
                 CA 282<br />PHP & MySQL=>ca282,
                 CA 288<br />Adv ColdFusion=>ca288,
                 CS 110<br />Computer Concepts=>cs110");

        echo "</ul>n";
        echo "</div>n";
    }

    function item($menuItem, $linkLocation, $subItems="") {
        echo "<li>n";
        echo "<a href='{$linkLocation}' title='{$menuItem}'>$menuItem</a>n";
            // Insert the sub-menu items if there is one
        if (!empty($subItems)) {
            $this->submenu($subItems);
        }
        echo "</li>n";
    }

    function submenu($itemaLink) {
        $itemaLinks = explode(",", $itemaLink);
        echo "<div class='submenu'>n";
        echo "<ul>n";
        for ($i=0; $i", $itemaLinks[$i]);
                    // This is used for my courses names
                $title = explode("<br />", $itemLink[0]);
                if(sizeof($title) == 1) {
                    $title[1] = $itemLink[1];
                }
                echo "<li>n";
                echo "<a href='{$itemLink[1]}' title='{$title[1]}'>{$itemLink[0]}</a>n";
                echo "</li>n";
            }
        }
        echo "</ul>n";
        echo "</div>n";
    }
}
?>

Note the $itemLink variable from the code above. This is based on the array format. Equal greater than (=>) symbol is variable and value connector in an array. This makes me easier to remember to add a link to the corresponding item. I also added $title array later in the process to differentiate between course title (e.x. CA 288) and course name (e.x. Advanced ColdFusion Development). You can see the example on my beta site. Note that I have not added any JavaScript to animate the menu. I will add MooTools-based animation later after I complete writing this menu class.

0

HTML Template PHP class

This is my first fully-written PHP class to ease my HTML page creation process by pre-set the DOCTYPE and other required tags in a normal HTML page. The class is inspired by this article where it talks about wp_header and wp_footer of WordPress. I thought that this is a great exercise to increase my OOP (Object-Oriented Programming) skill. Therefore I wrote the following code based on my creation habit for HTML pages:

<?php
    class template {
        public $title;
        public $style;

        function header($headerContent="") {
            echo "<!doctype html>n";
            echo "<html>n";
            echo "<head>n";
            echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
            echo "<title>";
            echo $this->title;
            echo "</title>n";
            if (isset($this->style) || !empty($this->style)) {
                echo "<link href='{$this->style}' rel='stylesheet' type='text/css' />n";
            }
                // Output additional header content if available
            if (function_exists($headerContent)) {
                $headerContent();
            }
            echo "</head>n";
        }

        function body($bodyContent="") {
            echo "<body>n";
                // Output body content
            if (function_exists($bodyContent)) {
                $bodyContent();
            }
        }

        function script($script='') {
            if (strstr($script, ".js") || strstr($script, "://")) {
                echo "<script type='text/javascript' src='{$script}'></script>n";
            }
            else {
                if (strstr($script, "()")) {
                    $function = str_replace("()", "", $script);
                    if (function_exists($function)) {
                        $function();
                    }
                }
                else {
                    echo "<script type='text/javascript'>n";
                    echo $script;
                    echo "</script>n";
                }
            }
        }

        function footer($footerContent="") {
            if (function_exists($footerContent)) {
                $footerContent();
            }
            echo "</body>n";
            echo "</html>n";
        }
    }
?>

The header, body, and footer methods include an optional string variable. As you can see in the code above, this variable needs to be a function name. The function name in these three methods doesn’t require parentheses. However, the function name needs to have parentheses in the script method. The script method is different from the above three methods as it can be reused in the same HTML page. For example, the following is a simple page with the class applied:

<?php
    $ca282 = new template();
    $ca282->title = "Robby Chen CA 282";
    $ca282->style = "styles/global.css";
    $ca282->header("headerContent");
    $ca282->body("bodyContent");
    $ca282->script("http://www.google.com/jsapi");
    $ca282->script("google.load('jquery','1')");
    $ca282->script("jquery()");
    $ca282->footer();
?>

Notice that the script method has three formats in the above example, external JS, on-line code, and a function. I didn’t pre-set the script tag between function because I need the syntax highlighting and auto-completion features in NetBeans. The following is my jquery() function in case you don’t know how to activate these two features within a PHP function:

<?php
    function jquery() {
?>
<script type="text/javascript">
    $(document).ready(function() {
        $("#logo").html("Logo");
    });
</script>
<?php
    }
?>

Basically, the JavaScript code is outside the php tag but inside the PHP function. This way the script content will only appear after the jquery function is called.

You can download the class file here.

<?php
class template {
public $title;
public $style;

function header($headerContent=”") {
echo “<!doctype html>n”;
echo “<html>n”;
echo “<head>n”;
echo “<meta http-equiv=’Content-Type’ content=’text/html; charset=utf-8′ />”;
echo “<title>”;
echo $this->title;
echo “</title>n”;
if (isset($this->style) || !empty($this->style)) {
echo “<link href=’{$this->style}’ rel=’stylesheet’ type=’text/css’ />n”;
}
// Output additional header content if available
if (function_exists($headerContent)) {
$headerContent();
}
echo “</head>n”;
}

function body($bodyContent=”") {
echo “<body>n”;
// Output body content
if (function_exists($bodyContent)) {
$bodyContent();
}
}

function script($script=”) {
if (strstr($script, “.js”) || strstr($script, “://”)) {
echo “<script type=’text/javascript’ src=’{$script}’></script>n”;
}
else {
if (strstr($script, “()”)) {
$function = str_replace(“()”, “”, $script);
if (function_exists($function)) {
$function();
}
}
else {
echo “<script type=’text/javascript’>n”;
echo $script;
echo “</script>n”;
}
}
}

function footer($footerContent=”") {
if (function_exists($footerContent)) {
$footerContent();
}
echo “</body>n”;
echo “</html>n”;
}
}
?>