Jump to content

Recommended Posts

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

/*********************************************************************************
*       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 = NULL, article $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
/*********************************************************************************
*       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
/*********************************************************************************
*       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 » Articles » '.$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

 

Link to comment
https://forums.phpfreaks.com/topic/137917-please-review-my-code/
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.