Location: PHPKode > scripts > printSimpelColumns > printsimpelcolumns/class.columns.php
<?
  /*
  **
  **  class printSimpelColumns
  **
  **  Author Carl Friis-Hansen
  **
  **  email: hide@address.com
  **
  **  Please use this as if it was your own invention.
  **  I have intentionally not stored all the rows in an array before sending to the client.
  **  For those who loved the C style text formatting, I have implemented this (I like it :-)
  **  The are two pre-defined columns, which you most likely need to re-define.
  **  Column definitions are numbered 1 to ...
  **
  **  Please don't confuse this with html tables.
  **  this class was constructed with the purpose of formatting tables when
  **  using php as a console application.
  **
  */




  /*
    See below object for detailed info about the specifier.
  */

  /*
  Example of how to use this class:

    $psc = new printSimpelColumns();
    $psc->setColumnDefinition(0, "EndDate"  ,"%-8s | ");
    $psc->setColumnDefinition(1, "EndTime"  ,"%-8s | ");
    $psc->setColumnDefinition(2, "Duration" ,"%-8s | ");
    $psc->setColumnDefinition(3, "Price"    ,"%7s | ");
    $psc->setColumnDefinition(4, "Name"     ,"%-8s | ");
    $psc->setColumnDefinition(5, "Digits"   ,"%-17s\r\n");

    print $psc->getHeaderLine();
    while($a  = doQuery())
    {
      print $psc->getDataLine($a);
    }
  */



  class printSimpelColumns {



    var $fh = array("", "");              // Initially 2 empty column headers
    var $fl = array("%-8s | ", "%8.2d");  // Initially 2 column format specifiers



    function  printSimpelColumns()
    {
      // Constructer.
    } // printSimpelColumns()



    function  setColumnDefinition($index, $header, $specifier)
    {
      $this->fh[$index] = $header;
      $this->fl[$index] = $specifier;
    } // setColumnDefinition()



    function  getHeaderLine()
    {
      $Header = "";
      for($n=0; $n<count($this->fh); $n++)
      {
        $Header .= sprintf($this->fl[$n], $this->fh[$n]);
      }
      return $Header;
    } // getHeaderLine()



    function  getDataField($index, $data)
    {
      return  sprintf($this->fl[$index], $data);
    } // getDataField()



    function  getDataLine($data)
    {
      $Line = "";
      for($n=0; $n<count($data); $n++)
      {
        $Line .= $this->getDataField($n, $data[$n]);
      }
      return  $Line;
    } // getDataLine()



  } //  printSimpelColumns



  /*
    Each conversion specification consists of a percent sign (%),
    followed by one or more of these elements, in order: 

    An optional padding specifier that says what character will be
    used for padding the results to the right string size.
    This may be a space character or a 0 (zero character).
    The default is to pad with spaces. An alternate padding character
    can be specified by prefixing it with a single quote ('). 

    An optional alignment specifier that says if the result should be
    left-justified or right-justified.
    The default is right-justified; a - character here will make it
    left-justified. 

    An optional number, a width specifier that says how many
    characters (minimum) this conversion should result in. 

    An optional precision specifier that says how many decimal digits
    should be displayed for floating-point numbers. This option has
    no effect for other types than float. (Another function useful
    for formatting numbers is number_format().) 

    A type specifier that says what type the argument data should be
    treated as. Possible types: 

    % - a literal percent character. No argument is required.  
    b - the argument is treated as an integer, and presented as a binary number.  
    c - the argument is treated as an integer, and presented as the character with that ASCII value.  
    d - the argument is treated as an integer, and presented as a (signed) decimal number.  
    u - the argument is treated as an integer, and presented as an unsigned decimal number.  
    f - the argument is treated as a float, and presented as a floating-point number.  
    o - the argument is treated as an integer, and presented as an octal number.  
    s - the argument is treated as and presented as a string.  
    x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).  
    X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).  
  */



  /*
    Example 4. Argument swapping

    $format = "The %2\$s contains %1\$d monkeys.
               That's a nice %2\$s full of %1\$d monkeys.";
    printf($format, $num, $location);


 
    Example 5. sprintf(): zero-padded integers

    $isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);


 
    Example 6. sprintf(): formatting currency

    $money1 = 68.75;
    $money2 = 54.35;
    $money = $money1 + $money2;
    // echo $money will output "123.1";
    $formatted = sprintf("%01.2f", $money);
    // echo $formatted will output "123.10"
  */



?>
Return current item: printSimpelColumns