quick table class - comments please

yzenezy <[email protected]>
Newsgroups gmane.comp.web.oscommerce.devel
Message-ID <5618e3b74d0c63cead9e0c87137de71f@osCommerce-Forums>
This message was sent from: Development
http://forums.oscommerce.com/viewtopic.php?p=162263#162263
----------------------------------------------------------------

Hi

I'm not sure if this is the right place for this.

I've written a class that creates a table from passed in array variables holding tag attributes and content for the table. I'm wondering if someone might review it and/or like to use it. Currently I use it to generate a 'banner search' box on my test osc website.

I'm new to php but want to learn a bit more. Any comments would be appreciated even if it's jjust to post elsewhere with this. 

[b]the class:
quick_table.php[/b]
<?php
/*
  $Id: quick_table.php,v 1.00 2003/05/04 14:38:00 tom Exp $
                                                                                
  thos.c.robinson
  [email protected]
                                                                                
  Copyright (c) 2003 thos.c.robinson
                                                                                
  Released under the GNU General Public License
*/
                                                                                
/* class draw_table
* draw a table
*       array(
*               'attrs'=>$default_td_attrs,
*               'contents'=>array('row 2, col 1', 'row 2, col 2')
*       )
*/
class quick_table {
        /* members */
        var $table_attrs = array("border" => "0", "cellspacing"=>'0', "cellpadding"=>'1', "align"=>"center");
        var $tr_attrs = array("align"=>"center", "valign"=>"middle");
        var $td_attrs = array("align"=>"center", "valign"=>"middle");
                                                                                
        /* class constructor
        */
        function quick_table() {
        }
                                                                                
        /* draw a table based on the contents of the array $td
        */
        function draw_table($table = '', $tr = '', $td = '') {
                if (sizeof($table['attrs']))
                        $table_attrs = $table['attrs'];
                else
                        $table_attrs = $this->table_attrs;
                                                                                
                if (sizeof($tr['attrs']))
                        $tr_attrs = $tr['attrs'];
                else
                        $tr_attrs = $this->tr_attrs;
                                                                                
                $this->begin_tag('table', $table_attrs);
                                                                                
                /* render tr's */
                for ($i=0; $i<sizeof($td); $i++) {
                        $this->begin_tag('tr', $tr_attrs);
                        //echo $tr_content[$i];
                        /* render td's */
                        $cc=$td[$i];
                        $contents=$cc['contents'];
                        for ($j=0; $j<sizeof($contents); $j++) {
                                if (sizeof($td['attrs']))
                                        $td_attrs = $td['attrs'];
                                else
                                        $td_attrs = $this->td_attrs;
                                $this->begin_tag('td', $td_attrs);
                                echo $contents[$j];
                                $this->end_tag('td');
                        }
                        $this->end_tag('tr');
                }
                $this->end_tag('table');
        }
                                                                                
        function array_merge_clobber($a1, $a2) {
                if(!is_array($a1) || !is_array($a2))
                        return false;
                $newarray = $a1;
                foreach ($a2 as $key => $val) {
                        if (is_array($val) && is_array($newarray[$key])) {
                                $newarray[$key] = array_merge_clobber($newarray[$key], $val);
                        } else {
                                $newarray[$key] = $val;
                        }
                }
                return $newarray;
        }
                                                                                
        /* merge the default table attributes
        */
        function merge_table_attrs($attrs = '') {
                $temp = $this->array_merge_clobber($this->table_attrs, $attrs);
                $this->table_attrs = $temp;
        }
                                                                                
        /* merge the default tr attributes
        */
        function merge_tr_attrs($attrs = '') {
                $temp = $this->array_merge_clobber($this->tr_attrs, $attrs);
                $this->tr_attrs = $temp;
        }
                                                                                
        /* merge the default td attributes
        */
        function merge_td_attrs($attrs = '') {
                $temp = $this->array_merge_clobber($this->td_attrs, $attrs);
                $this->td_attrs = $temp;
        }
                                                                                
        /* set the default table attributes
        */
        function set_table_attrs($attrs = '') {
                $this->table_attrs = $attr;
        }
                                                                                
        /* set the default tr attributes
        */
        function set_tr_attrs($attrs = '') {
                $this->tr_attrs = $attr;
        }
                                                                                
        /* set the default td attributes
        */
        function set_td_attrs($attrs = '') {
                $this->td_attrs = $attr;
        }
                                                                                
        /* parse tag attributes out of an array and
        * return a concatenated string equivalent
        */
        function parse_tag_attributes($attrs) {
                if (!is_array($attrs)) {
                        $attrs = array();
                }
                $attrs_str = '';
                if (is_array($attrs) && (sizeof($attrs) > 0)) {
                  reset($attrs);
                  while (list($key, $value) = each($attrs)) {
                        if ((strlen($value) > 0)) {
                          $attrs_str .= $key . '="' . $value . '" ';
                        }
                  }
                }
                return $attrs_str;
        }
                                                                                
        /* create the opening tag of a tag set
        */
        function begin_tag($tag, $tag_attrs = '') {
                /* parse table attributes */
                $tag_attrs_str = $this->parse_tag_attributes($tag_attrs);
                echo '<'.$tag.' '.$tag_attrs_str.'>';
        }
                                                                                
        /* create the closing tag of a tag set
        */
        function end_tag($tag) {
                echo '</'.$tag.'>';
        }
}
                                                                                
?>

[b]the usage:[/b]
<!-- banner_search //-->
                                                                                
<?php
//"bordercolor" => "#FF0000", 'class' => 'header', 'width' => '50%'
//"bordercolor" => "#00FF00", "bordercolor" => "#0000FF", "border" => "1", "cellspacing"=>'4', "bordercolor" => "#FF0000"
                                                                                
/* begin_form_tag */
echo tep_draw_form('quick_find', tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT,
'', 'NONSSL', false), 'get');
$table = new quick_table();
$table->merge_td_attrs(array("class" => "header", "valign" => "top"));
$table->draw_table(/*table*/ array(), /*tr*/ array(),
        /*td*/
        array(
                array(
                        //'attrs'=>'',
                        'contents'=>array(
                                'search&nbsp;' . tep_draw_input_field('keywords', '', 'size="10" maxlength="30" style="width: ' . (BOX_WIDTH-30) . 'px"') . '</input>&nbsp;' . tep_image_submit('button_quick_find.gif', BOX_HEADING_SEARCH) . '</input>' . ((tep_not_null(($sid = tep_hide_session_id()))) ? $sid . '</input>'
: ''),
                                '<a href="' . tep_href_link(FILENAME_ADVANCED_SEARCH) . '">' . tep_image(DIR_WS_LANGUAGES . '/english/images/buttons/button_quick_find.gif', BOX_SEARCH_ADVANCED_SEARCH) . '</img></a>'
                        )
                )
        )
);
?>
                                                                                
</form>
                                                                                
<!-- banner_search_eof //-->


[b]the output:[/b]
<form name="quick_find" action="http://olympus/catalog/advanced_search_result.php" method="get"><table border="0" cellspacing="0" cellpadding="1" align="center" ><tr align="center" valign="middle" ><td align="center" valign="top" class="header" >search&nbsp;<input type="text" name="keywords" size="10" maxlength="30" style="width: 110px"></input>&nbsp;<input type="image" src="includes/languages/english/images/buttons/button_quick_find.gif" border="0" alt="Quick Find" title=" Quick Find "></input><input type="hidden" name="osCsid" value="3f5dcdefdad6999a51eb5de38b661f4e"></input></td><td align="center" valign="top" class="header" ><a href="http://olympus/catalog/advanced_search.php?osCsid=3f5dcdefdad6999a51eb5de38b661f4e"><img src="includes/languages//english/images/buttons/button_quick_find.gif" border="0" alt="Advanced Search" title=" Advanced Search " width="16" height="17"></img></a></td></tr></table>
</form>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.