Version: 1.3
Type: Class
Category: HTML
License: GNU General Public License
Description: PHP Class to build tables without tag “<table>”. Usefull for building text/plain tables.
<? # class.tb_simul.php # PHP Class to build tables without tag "<table>" # Warning: Use only with monospace fonts! # benadin <[email protected]> , www.benadin.info # Version 1.3 # # Copyright (c) 2005 Alexander S. Filatov # Permission granted to use and modify this library so long as the # copyright above is maintained, modifications are documented # Usage # # include_once "class.tb_simul.php"; # $tb = new tb_simul(array ( # 'col 1' => 3, // define column 'col 1' 3 symbols width # 'col_2' => 10, # 'col_3' => 5, # )); # $tb->row_sep_flag = 0; # $tb->br = "rn"; # $tb->echo_mode = 0; # $tb->build_header(); # $tb->build_row(array("#1", "this is row 1", "...")); # $tb->build_row(array("#2", "this - row 2", "text will be wrapped!")); # $tb->build_footer(); # echo $tb->result; class tb_simul { var $row_sep_flag; // show or not rows separator, default 0 var $result; // if no direct echo, lets store to $this->total_tb function tb_simul($tb_fields=array(), $sep="|", $sep2 = "-", $br = "<br>", $space=" ", $echo_mode = 1, $html_mode = 1) { $this->tb_fields = $tb_fields; // array of tb_field => width //$this->tb_cols_count = sizeof($this->tb_fields); $this->sep = $sep; // separator between columns; $this->sep2 = $sep2; // separator between rows; $this->br = $br; $this->space = $space; $this->echo_mode = $echo_mode; $this->tb_total_width = 1; $this->v_line = "+"; $this->row_sep_flag = 0; foreach ($this->tb_fields as $tb_field => $width) { $this->v_line .= str_repeat($this->sep2, $width + strlen($this->sep) - 1)."+"; $this->tb_total_width += $width + strlen($this->sep); } $this->reset_data(); } function reset_data() { $this->result = ""; } function set_txt_mode() { $this->html_mode = 0; $this->br = "rn"; $this->space = " "; } function add($txt) { if ($this->echo_mode) echo $txt; else $this->result .= $txt; } function build_header() { $this->add( $this->v_line() ); $this->build_simple_row(array_keys($this->tb_fields)); $this->add( $this->v_line() ); } function build_footer() { if (!$this->row_sep_flag) $this->add( $this->v_line() ); } function build_simple_row($data_arr) { $data_arr = $this->padding_arr($data_arr); $i = 0; $this->add( $this->sep ); foreach ($this->tb_fields as $tb_field => $width) $this->add( $this->fix_str($data_arr[$i++], $width) ); $this->add( $this->br ); } function build_row($data_arr) { $data_arr = $this->padding_arr($data_arr); $new_data_arr = array(); $new_data_flag = 0; $i = 0; $this->add( $this->sep ); foreach ($this->tb_fields as $tb_field => $width) { $data_item = $data_arr[$i++]; $sub_item = ""; if (strlen($data_item) > $width) { $sub_item = substr($data_item, $width - 1); // -1 - to padd 1 $data_item = substr($data_item, 0, $width - 1); $new_data_flag = 1; } $new_data_arr[] = $sub_item; $this->add( $this->fix_str($data_item, $width) ); } $this->add( $this->br ); if ($this->row_sep_flag && !$new_data_flag) $this->add( $this->v_line() ); if ($new_data_flag) // recursive $this->build_row($new_data_arr); } function build_rows($rows_arr) { foreach ($rows_arr as $row_data) $this->build_row($row_data); } function v_line() { return $this->v_line.$this->br; //return str_repeat($this->sep2, $this->tb_total_width); } function fix_str($text, $width) { $text = $text; if ($this->html_mode) $text = htmlspecialchars($text); $count = $width - strlen($text); $text = str_replace(array("rn","t"), array($this->space.$this->space, $this->space), $text); if ($count < 0) $count = 0; return $text.str_repeat($this->space, $count).$this->sep; } function padding_arr($data_arr) { $pad_data_arr = array(); foreach ($data_arr as $item) $pad_data_arr[] = $this->padding($item); return $pad_data_arr; } # making space padding function padding($txt) { return $this->space.$txt.$this->space; } } ?>