View Single Post
Old 12-21-2008, 08:59 PM   #1 (permalink)
geo353
The Wanderer
 
geo353's Avatar
 
Join Date: Dec 2008
Location: Wiltshire, UK
Posts: 13
Thanks: 0
geo353 is on a distinguished road
Big Grin How good is my code

Hello,

Ive been programming in php for a few years now but never really took any notice of how practical or versatile my code is. Im currently working on building myself a portfolio site (something ive been meaning to for a long time). Please take a look at a couple of snippets of my code and give me some feedback.

PHP Code:
<?php

/*********************************************************************************
 *       Filename: portfolioManager.lib.php
 *       Version 1.0
 *       Copyright 2007 - 2009 (c) George Palmer
 *       Last modified: 20 Dec 2008
 *********************************************************************************/

require_once('../inc/lib/dbManager.lib.php');
/*********************************************************************************/
    
class article implements IteratorAggregate
    
{
        protected 
$article;
        
        public function 
__construct($article NULL)
        {
                
$this->article $article;
        }
        
        public function 
__get($property)
        {
            if(isset(
$this->article[$property]))
            {
                return 
$this->article[$property];
            } else {
                return 
false;
            }
        }
        
        public function 
__set($property$value)
        {    
                
$this->article[$property] = $value;
        }
        
        public function 
getIterator()
        {
            return new 
ArrayObject($this->article);

        }
    }
/*********************************************************************************/
    
class portfolio implements IteratorAggregate
    
{
        protected 
$articles;
        
        public function 
__construct($articles NULL)
        {
            if(
$articles != NULL)
            {
                
array_push($this->articles$articles);
            }
        }    
        
        public function 
displayALL($template)
        {
            foreach (
$this->getIterator() as $article)
            {
                
$articleT = new templater($template);
                
$articleT->build($article->getIterator());
                
$articleT->publish();
            }
        }
        
        public function 
displayRand($template)
        {
            
$key array_rand($this->articles);
            
$article $this->articles[$key];
            
$articleT = new templater($template);
            
$articleT->build($article->getIterator());
            
$articleT->publish();
        }
        
        public function 
__set($articles NULLarticle $article)
        {
            
$this->articles[] = $article;
        }
        
        public function 
getIterator()
        {
            return new 
ArrayObject($this->articles);
        }
    }
/*********************************************************************************/

?>
The article class is takes a list of properties and their values from the database row. This object is then stored in the portfolio object for each row.

Depending on the page and result set the articles are displayed like so.

PHP Code:
/*********************************************************************************
 *       Filename: home.inc.php
 *       Version 1.0
 *       Copyright 2007 - 2009 (c) George Palmer
 *       Last modified: 21 Dec 2008
 *********************************************************************************/
    //header template
        
$headerT = new templater('templates/header.tem.php');
            
$headerV = array('TITLE' => page::getPageName(false),
                             
'HEAD'     => page::getPageName(true));
            
$headerV = new ArrayObject($headerV);
        
$headerT->build($headerV);
    
//end header template
    
    //footer template
        
$footerT = new templater('templates/footer.tem.php');
            
$footerV = array('FOOT' => 'Your viewing the '.page::getPageName(true).' page on '.date("D jS, g:i a").'.');
            
$footerV = new ArrayObject($footerV);
        
$footerT->build($footerV);
    
//end footer template
/*********************************************************************************/
    //libarys required
        
require_once('../inc/lib/portfolioManager.lib.php');
    
//end libarys
/*********************************************************************************/
    //start page
        
$dbPortfolio = new portfolioConnect//connect to the portfolio database
        
$articles $dbPortfolio->fetchAll();//fetch all articles
        
        
$headerT->publish();//publish header
        
        
$articles->displayRand('templates/articleSingle.tem.php');//display all articles using template
        
        
$articles->displayAll('templates/articleAll.tem.php');//display all articles using template
        
        
$footerT->publish();//publish footer
    //end page
/*********************************************************************************/ 
or

PHP Code:
/*********************************************************************************
 *       Filename: home.inc.php
 *       Version 1.0
 *       Copyright 2007 - 2009 (c) George Palmer
 *       Last modified: 21 Dec 2008
 *********************************************************************************/
    //libarys required
        
require_once('../inc/lib/portfolioManager.lib.php');
    
//end libarys
/*********************************************************************************/
    //start page before header template in order to fetch title
        
$articleID $_GET['a']; //unclean!!!!!!!!!!!
        
        
$dbPortfolio = new portfolioConnect//connect to the portfolio database
        
$article $dbPortfolio->fetchSingle($articleID);//fetch single article
    //part end page
/*********************************************************************************/
    //header template
        
$headerT = new templater('templates/header.tem.php');
            
$headerV = array('TITLE' => 'George Palmer &raquo; Articles &raquo; '.$article->title,
                             
'HEAD'     => $article->title);
            
$headerV = new ArrayObject($headerV);
        
$headerT->build($headerV);
    
//end header template
    
    //footer template
        
$footerT = new templater('templates/footer.tem.php');
            
$footerV = array('FOOT' => 'Your viewing the "'.$article->title.'" article on '.date("D jS, g:i a").'.');
            
$footerV = new ArrayObject($footerV);
        
$footerT->build($footerV);
    
//end footer template
/*********************************************************************************/
    //start page
        
        
$headerT->publish();//publish header
        
        
$articleT = new templater('templates/articleDetailed.tem.php');
        
$articleT->build($article->getIterator());
        
$articleT->publish();
        
        
$footerT->publish();//publish footer
    //end page
/*********************************************************************************/
?> 
Im also looking for a way to implement paging into this, if anyone has any suggestions.

Many Thanks
George Palmer

Last edited by geo353 : 12-21-2008 at 09:10 PM. Reason: cant spell code lol
geo353 is offline  
Reply With Quote