home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / function.html_table.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  2.4 KB  |  100 lines

  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * @package Smarty
  5.  * @subpackage plugins
  6.  */
  7.  
  8.  
  9. /**
  10.  * Smarty {html_table} function plugin
  11.  *
  12.  * Type:     function<br>
  13.  * Name:     html_table<br>
  14.  * Date:     Feb 17, 2003<br>
  15.  * Purpose:  make an html table from an array of data<br>
  16.  * Input:<br>
  17.  *         - loop = array to loop through
  18.  *         - cols = number of columns
  19.  *         - table_attr = table attributes
  20.  *         - tr_attr = table row attributes (arrays are cycled)
  21.  *         - td_attr = table cell attributes (arrays are cycled)
  22.  *         - trailpad = value to pad trailing cells with
  23.  * 
  24.  * Examples:
  25.  * <pre>
  26.  * {table loop=$data}
  27.  * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
  28.  * {table loop=$data cols=4 tr_attr=$colors}
  29.  * </pre>
  30.  * @author     Monte Ohrt <monte@ispi.net>
  31.  * @version  1.0
  32.  * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
  33.  *          (Smarty online manual)
  34.  * @param array
  35.  * @param Smarty
  36.  * @return string
  37.  */
  38. function smarty_function_html_table($params, &$smarty)
  39. {
  40.     $table_attr = 'border="1"';
  41.     $tr_attr = '';
  42.     $td_attr = '';
  43.     $cols = 3;
  44.     $trailpad = ' ';
  45.     
  46.     extract($params);
  47.  
  48.     if (!isset($loop)) {
  49.         $smarty->trigger_error("html_table: missing 'loop' parameter");
  50.         return;
  51.     }
  52.     
  53.     $output = "<table $table_attr>\n";
  54.     $output .= "<tr " . smarty_function_html_table_cycle('tr', $tr_attr) . ">\n";
  55.  
  56.     for($x = 0, $y = count($loop); $x < $y; $x++) {
  57.         $output .= "<td " . smarty_function_html_table_cycle('td', $td_attr) . ">" . $loop[$x] . "</td>\n";        
  58.         if((!(($x+1) % $cols)) && $x < $y-1) {
  59.             // go to next row
  60.             $output .= "</tr>\n<tr " . smarty_function_html_table_cycle('tr', $tr_attr) . ">\n";
  61.         }
  62.         if($x == $y-1) {
  63.             // last row, pad remaining cells
  64.             $cells = $cols - $y % $cols;
  65.             if($cells != $cols) {
  66.                 for($padloop = 0; $padloop < $cells; $padloop++) {
  67.                     $output .= "<td " . smarty_function_html_table_cycle('td', $td_attr) . ">$trailpad</td>\n";
  68.                 }
  69.             }
  70.             $output .= "</tr>\n";
  71.         }
  72.     }
  73.             
  74.     $output .= "</table>\n";
  75.     
  76.     return $output;
  77. }
  78.  
  79. function smarty_function_html_table_cycle($name, $var) {
  80.     static $names = array();
  81.     
  82.     if(!is_array($var)) {
  83.         return $var;
  84.     }
  85.     
  86.     if(!isset($names[$name]) || $names[$name] == count($var)-1) {
  87.         $names[$name] = 0;
  88.         return $var[0];
  89.     }
  90.     
  91.     $names[$name]++;
  92.     return $var[$names[$name]];
  93.     
  94. }
  95.  
  96.  
  97. /* vim: set expandtab: */
  98.  
  99. ?>
  100.