Notes & Code Snippets

Read notes about design & development, or useful scripts and bits of code from me

Feedback generation

March 29, 2011

Keywords: php feedback

I use this class in everything I do. It's essential for validating user input, confirming actions, etc.
 
///////////////////////////////////////////////////////
//////  THIS CODE DEVELOPED BY BENJAMIN PITTMAN ///////
//////              www.bpittman.com            ///////
///////////////////////////////////////////////////////
 
class Feedback{
    private $type = 1;
    public $items = array();
    private $style = 1;
    private $label;
    
    function add($str){
        array_push($this->items, $str);
    }
    
    function type($str){
        if( $str == 'success' || $str == 1 )
            $this->type = 1;
        else if ($str == 'failure' || $str == 'fail' || $str == 'error' || $str == 'err' || $str == 2)
            $this->type = 2;
    }
    
    function style($str){
        if( $str == 'list' || $str == 1 )
            $this->style = 1;
        else if( $str == 'block' || $str == 2 )
            $this->style = 2;
    }
    
    function label($str){
        $this->label = $str;
    }
    
    function isEmpty(){
        if( count($this->items) > 0 )
            return false;
        else
            return true;
    }
    
    function execute(){
        $html = "";
        if( !$this->label )
            return false;
        $html .= "<div class='feedback'>\n";
        if( $this->type == 1 )
            $html .= "<div class='success'>\n";
        else if( $this->type == 2 )
            $html .= "<div class='failure'>\n";
        $html .= "<h1>" . $this->label . "</h1>\n";
        if( count($this->items) > 0 ){
            if( $this->style == 1 ){
                $html .= "<ul>\n";
                foreach( $this->items as $k=>$v ){
                    $html .= "<li>" . $v . "</li>\n";
                }
                $html .= "</ul>\n";
            }
            else if( $this->style == 2 ){
                foreach( $this->items as $k=>$v ){
                    $html .= "<p>" . $v . "</p>\n";
                }
            }
        }
        $html .= "</div>\n";
        $html .= "</div>\n";
        return $html;
    }
    
}

Back to top