TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-21-2008, 08:59 PM   #1 (permalink)
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
Old 12-21-2008, 09:27 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

For one thing, I like that it's clean
Don't have time right now to comment the actual code.
But here's a pagination class that I wrote if you're interested!

Pagination class

You'll probably find a really easy way to integrate this!
Keep on coding!
__________________
Tanax is offline  
Reply With Quote
Old 12-22-2008, 05:00 PM   #3 (permalink)
The Wanderer
 
Join Date: Jul 2008
Posts: 5
Thanks: 0
Crazymik3 is on a distinguished road
Default

You're code is nice, put it seems very thick.

Maybe space it out a little more, as it looks really congested right now with so many comments.
Crazymik3 is offline  
Reply With Quote
Old 12-22-2008, 07:39 PM   #4 (permalink)
The Wanderer
 
geo353's Avatar
 
Join Date: Dec 2008
Location: Wiltshire, UK
Posts: 13
Thanks: 0
geo353 is on a distinguished road
Default

cheers tanax i'll have a look at that. crazymik3 iknow its really thick its just how i like to work, means i can fit more on my screen. as for the comments, good point i dont really need most of them, if any.

cheers
geo353 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Good PHP Book Recommendation CMellor The Lounge 4 06-23-2008 11:28 AM
i need some good ideas about my website webtuto General 4 03-14-2008 12:33 PM
Good business communication. Village Idiot The Lounge 10 01-11-2008 08:43 PM
Good 'n Fast thumbnail script?! marxx General 6 10-31-2007 11:22 AM
Thank you very much for such a good work KanaomiSS General 0 09-11-2006 06:59 AM


All times are GMT. The time now is 11:06 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design