Classe para construir tabelas

02.05.2008 | Categoria: Blog

Num dia complicado como este, vale a minha descoberta desta bela classe escrita em PHP:

O objectivo que eu tinha em mente era construir tabelas de forma dinâmica, sem me preocupar com tags html: apenas ter que inserir o conteúdo das células.

E encontrei esta no PHPClasses (http://phpclasses.org), que permite fazer tabelas, adicionando linha a linha, e incluir atributos de células e de linhas… Ideal!

O Sr que a escreveu chama-se Jason Lotito, é do Canadá e salvou o meu dia.

A sua utilização é tão simples como isto:

	$table = new Table();
	$table->SetTableAttributes( array( "width" => "600", "border" => "1", "align" => "center", "cellpadding" => "4" ) );
	$table->SetDefaultCellAttributes( array("bgcolor" => "white", "align" => "center" ) );
 
	$row = $table->AddRow();
	$table->SetCellColSpan( $row, 1, 10 );
 
	$table->SetCellContent( $row, 1, "Linha 1" );
	$table->SetCellContent( $row, 2, "Linha 2" );
	$table->SetCellContent( $row, 3, "Linha 3" );
 
       $table->PrintTable();

et Voilá!

A classe é a seguinte:

table = array();
        $this->default_settings = array();
        $this->row_count = 0;
        $this->default_settings = array();
        $this->fstyles = array();
    }
 
    /**
    * Adds 1 to the row_count count, keeping track of the total
    * number of rows in the table.  It returns the current
    * row_count after the addition.
    *
    * @access    public
    * @return    int        Returns the current number of rows
    */
    function AddRow ()
    {
        return ++$this->row_count;
    }
 
    /**
    * Returns the current row you are working with.
    *
    * @access    public
    * @return    int        Returns the current number of rows.
    */
    function GetCurrentRow ()
    {
        return $this->row_count;
    }
 
    /**
    * Returns the current total number of columns in your table
    *
    * @access    public
    * @return    int        Returns the current number of rows.
    */
    function GetCurrentCols ()
    {
        $high = 0;
        foreach ( $this->table as $row => $array )
        {
            $cnt = count( $array );
            if ( $cnt > $high || $high == 0 )
            {
                $high = $cnt;
            }
        }
        return $high;
    }
 
    /**
    * Set the cell's content.
    *
    * Allows you to easily set the content of a cell.  The first
    * parameter required is the row the cell is on, followed by
    * the column the cell is in, and then finally, what the
    * content of the cell is.
    *
    * @access    public
    * @param    int        $row
    * @param    int        $col
    * @param    string    $content
    */
    function SetCellContent ( $row, $col, $content )
    {
        $this->table[$row][$col]["content"] = $content;
    }
 
    function SetRowContent ( $row, $content )
    {
        $count = count($content);
        $content_keys = array_keys($content);
        for ( $x = 0; $x < $count; $x++ )
        {
            $this->table[$row][( $x + 1 )]["content"] = $content[$content_keys[$x]];
        }
    }
 
    function SetColContent ( $col, $content )
    {
        $count = count($content);
        $content_keys = array_keys($content);
        for ( $x = 0; $x < $count; $x++ )
        {
            $this->table[( $x + 1 )][$col]["content"] = $content[$content_keys[$x]];
        }
    }
 
    /**
    * Sets the default content for all cells.
    *
    * @access    public
    * @param    string $content
    */
    function SetDefaultCellContent ( $content )
    {
        $this->default_settings['td']['content'] = $content;
    }
 
    /**
    * Set the cells BBGCOLOR setting
    * Allows you to easily set the bgcolor of the cell.
    * This works the same as the SetCellContent method.
    *
    * @access    public
    * @param    int        $row
    * @param    int        $col
    * @param    string    $bgcolor
    */
    function SetCellBGColor ( $row, $col, $bgcolor )
    {
        $this->table[$row][$col]["bgcolor"] = $bgcolor;
    }
 
    /**
    * Sets the default bgcolor for all cells.
    *
    * @access    public
    * @param    string    $bgcolor
    */
    function SetDefaultBGColor ( $bgcolor )
    {
        $this->default_settings['td']['bgcolor'] = $bgcolor;
    }
 
    /**
    * Allows you to easily set the style of the cell using CSS.
    * This works the same as the SetCellContent method.
    *
    * @access    public
    * @param    int        $row
    * @param    int        $col
    * @param    string    $style
    */
    function SetCellStyle ( $row, $col, $style )
    {
        $this->table[$row][$col]["style"] = $style;
    }
 
    /**
    * Sets the default style for all cells.
    *
    * @access    public
    * @param    string    $style
    */
    function SetDefaultStyle ( $style )
    {
        $this->default_settings['td']['style'] = $style;
    }
 
    /**
    * Allows you to easily set the colspan attribute of the cell
    * if need be.  This works the same as the SetCellContent
    * method.
    *
    * @access    public
    * @param    int        $row
    * @param    int        $col
    * @param    int        $colspan
    */
    function SetCellColSpan ( $row, $col, $colspan )
    {
        $this->table[$row][$col]["colspan"] = $colspan;
    }
 
    /**
    * Allows you to easily set the rowspan attribute of the cell
    * if need be.  This works the same as the SetCellContent
    * method.
    *
    * @access    public
    * @param    int        $row
    * @param    int        $col
    * @param    int        $rowspan
    */
    function SetCellRowSpan ( $row, $col, $rowspan )
    {
        $this->table[$row][$col]["rowspan"] = $rowspan;
    }
 
    /**
    * Allows you to easily set any one attribute of the cell
    * if need be.  This works the same as the SetCellContent
    * method.
    *
    * @access    public
    * @param    int        $row
    * @param    int        $col
    * @param    string    $attribute
    * @param    string    $value
    * @return    void
    */
    function SetCellAttribute ( $row, $col, $attribute, $value )
    {
        $this->table[$row][$col][$attribute] = $value;
    }
 
    /**
    * Sets the default attribute for all cells.
    *
    * @access    public
    * @param    string    $attribute
    * @param    string    $value
    * @return    void
    */
    function SetDefaultCellAttribute ( $attribute, $value )
    {
        $this->default_settings['td'][$attribute] = $value;
    }
 
    /**
    * Allows you to set multiple attributes for a cell in one
    * method call.  You would call it like this:
    * $attributes = array ( "attribute_name" => "value",
    * "attribute_name_2" => "value");
    * $table->SetCellAttributes( $row, $col, $attributes );
    *
    * @access    public
    * @param    int        $row
    * @param    int        $col
    * @param    array    $array
    * @return    void
    */
    function SetCellAttributes ( $row, $col, $array )
    {
        if ( is_array($array) )
        {
            foreach ( $array as $attribute => $value )
            {
                $this->table[$row][$col][$attribute] = $value;
            }
        }
    }
 
    /**
    * Set td default attributes
    *
    * Same setup as SetCellAttributes, however, without the row or
    * col parameters (however, the array still works the same way)
    * 'type' is what you are setting default attributes for.
    * Default attributes can be overriden simply by using one of
    * the aboe methods.  If you specifically set an attribute
    * using the 'row', 'col', 'attribute' methods above, they will
    * override the Default Attribute for that one element.
    *
    * @access    public
    * @param    array    $array
    */
    function SetDefaultCellAttributes ( $array )
    {
        foreach ( $array as $key => $value )
        {
            $this->default_settings["td"][$key] = $value;
        }
    }
 
    /**
    * Set table default attributes
    *
    * Same setup as SetCellAttributes, however, without the row or
    * col parameters (however, the array still works the same way)
    * 'type' is what you are setting default attributes for.
    * Default attributes can be overriden simply by using one of
    * the aboe methods.  If you specifically set an attribute
    * using the 'row', 'col', 'attribute' methods above, they will
    * override the Default Attribute for that one element.
    *
    * @access    public
    * @param    array    $array
    */
    function SetDefaultTableAttribute ( $array )
    {
        foreach ( $array as $key => $value )
        {
            $this->default_settings["table"][$key] = $value;
        }
    }
 
    /**
    * Set table attributes
    *
    * Sets the table attributes in the same manner as the
    * SetCellAttributes array.  The table information is stored
    * in the table array as the first element ($this->table[0]).
    *
    * @access    public
    * @param    array    $array
    * @return    void
    */
    function SetTableAttributes ( $array )
    {
        foreach ( $array as $key => $value )
        {
            $this->table[0]["table_values"][$key] = $value;
        }
    }
 
    /**
    * Set table attribute
    *
    * Sets the table attributes in the same manner as the
    * SetCellAttributes array.  The table information is stored
    * in the table array as the first element ($this->table[0]).
    *
    * @access    public
    * @param    array    $array
    * @return    void
    */
    function SetTableAttribute ( $key, $value )
    {
        $this->table[0]["table_values"][$key] = $value;
    }
 
    /**
    * Set attributes to particular rows.
    *
    * You can easily setup attributes for an entire row using this method.  You
    * simply send it a row number (or an array of row numbers if more than one)
    * as well as the attributes in an array format (like we do every place else)
    * and all the cells in the row number will be set with those attributes.
    * Styles set in this manner can be overridden by
    *
    * @access    public
    * @param    int or array        $row
    * @param    array                $attributes
    */
    function SetFancyRowStyle ( $row, $attributes )
    {
        if ( is_array( $row ) )
        {
            foreach ( $row as $num )
            {
                foreach( $attributes as $key => $value )
                {
                    $this->fstyles["row"][$num][$key] = $value;
                }
            }
        } else {
            foreach( $attributes as $key => $value )
            {
                $this->fstyles["row"][$row][$key] = $value;
            }
        }
    }
 
    /**
    * Set attributes to particular column.
    *
    * This works the same way as the SetFancyRowStyle method, except that this
    * method works on arrays.  This method will override SetFancyRowStyle styles
    * which are set.
    *
    * @access    public
    * @param    int || array        $row
    * @param    array                $attributes
    */
    function SetFancyColStyle ( $col, $attributes )
    {
        if ( is_array( $col ) )
        {
            foreach ( $col as $num )
            {
                foreach( $attributes as $key => $value )
                {
                    $this->fstyles["col"][$num][$key] = $value;
                }
            }
        } else {
            foreach( $attributes as $key => $value )
            {
                $this->fstyles["col"][$col][$key] = $value;
            }
        }
    }
 
    /**
    * Set alternating colors.
    *
    * You simply put in two HTML compatible colors, like #ffffff, or 'white' and
    * it will creat alternating color rows.
    *
    * @access    public
    * @param    string    $odd_colors        The odd numbered rows bgcolor value
    * @param    string    $even_colors    The even numbered rows bgcolor value
    * @param    int        $start            What row to start outputting the alternating colors on. Defaults to 1 (the first row).
    * @param    int        $end            What row to stop outputting the alternating colors on.  Defaults to the GetCurrentRow() value
    *
    */
    function Set2RowColors( $odd_colors, $even_colors, $start=1, $end=false )
    {
        if( $end === false )
        {
            $end = $this->GetCurrentRow();
        }
        for( $row = $start; $row <= $end; $row++ )
        {
            if ( ( $row % 2 ) != 0 )
            {
                $this->fstyles["row"][$row]["bgcolor"] = $odd_colors;
            } else {
                $this->fstyles["row"][$row]["bgcolor"] = $even_colors;
            }
        }
    }
 
    /**
    * Compile table to HTML.
    *
    * Turns the table array into an HTML table.  This must be
    * called before a table can be printed out.
    *
    * @access    public
    * @return    string    Returns a string of the table in HTML Format
    */
    function CompileTable ()
    {
        $content = "\n
default_settings["table"] ) )
        {
            $t_array = $this->default_settings["table"];
            foreach ( $t_array as $attribute => $value )
            {
                $this->table[0]["table_values"][$attribute] = $value;
            }
        }
 
        if ( isset( $this->table[0]["table_values"] ) )
        {
            $t_array = $this->table[0]["table_values"];
            foreach ( $t_array as $attribute => $value )
            {
                $content .= ' '.$attribute.'="'.$value.'"';
            }
        }
 
        $content .= ">\n";
 
        for ( $row = 1; $row <= $this->row_count; $row++ )
        {
 
            $content .= "\t
\n";
            $row_array = $this->table[$row];
 
            /*
            $td_array = $this->default_settings["td"];
            $frowstyle = $this->fstyles["row"][$row];
            if ( is_array( $frowstyle ) )
            {
                foreach ( $frowstyle as $attribute => $value )
                {
                    $td_array[$attribute] = $value;
                }
            }*/
 
            $count = count( $row_array );
            for ( $col = 1; $col <= $count; $col++ )
            {
                $colvalue = array();
                $colvalue = $row_array[$col];
                $content .= "\t\t
default_settings["td"];
                if ( isset($this->fstyles["row"][$row]) )
                {
                    $frowstyle = $this->fstyles["row"][$row];
                } else {
                    $frowstyle = "";
                }
                if ( is_array( $frowstyle ) )
                {
                    foreach ( $frowstyle as $attribute => $value )
                    {
                        $td_array[$attribute] = $value;
                    }
                }
 
                if ( isset( $this->fstyles["col"][$col] ) && !empty( $this->fstyles["col"][$col] ) )
                {
                    $fcolstyle = $this->fstyles["col"][$col];
                    if ( is_array( $fcolstyle ) )
                    {
                        foreach ( $fcolstyle as $attribute => $value )
                        {
                            $td_array[$attribute] = $value;
                        }
                    }
                }
 
                if ( is_array( $td_array ) )
                {
                    foreach ( $td_array as $attribute => $value )
                    {
                        if ( empty( $colvalue[$attribute] ) || !isset( $colvalue[$attribute] ) )
                        {
                            $colvalue[$attribute] = $value;
                        }
                    }
                }
                foreach ( $colvalue as $attribute => $value)
                {
                    if ( $attribute == "content" )
                    {
                        $t_string = $value;
                    } else {
                        $content .= " $attribute=\"$value\"";
                    }
                }
                $content .= ">\n";
                $content .= "\t\t\t".$t_string."\n";
                $content .= "\t\t
 
\n";
            }
            $content .= "\t
 
\n";
        }
        $content .= "
 
\n";
        return $content;
    }
 
    /**
    * Prints out the compiled table.
    *
    * @access    public
    */
    function PrintTable ()
    {
        echo $this->CompileTable();
    }
 
    /**
    * Allows you to quickly see what is located and where it's
    * located in your table array.  Used for debugging purposes.
    *
    * @access    dev
    */
    function printTableArray()
    {
        //Remove the ... on writing the code
        echo "<... pre ....>";
        print_r( $this );
        echo "<!-- ....pre...-->";
    }
}
 
/*
*
*    DOCINFO
*        TABSIZE:            4 SPACES
*        TAB_OR_SPACE:        TAB
*        LANGUAGE:            PHP
*        EDITOR:            EditPlus
*/
?&gt;

Mais informações aqui: http://www.phpclasses.org/browse/package/318.html

Boas «phpzadas»!

Tags: ,

No Comments

Comments are closed.