PHP Classes

File: examples/printDataInCSV.php

Recommend this page to a friend!
  Classes of William Sant Ana  >  Tables for the Shell  >  examples/printDataInCSV.php  >  Download  
File: examples/printDataInCSV.php
Role: Example script
Content type: text/plain
Description: Example of print a csv file on the console
Class: Tables for the Shell
Output tables of data rendered as text characters
Author: By
Last change:
Date: 8 years ago
Size: 1,417 bytes
 

Contents

Class file image Download
<?php

    $time
= microtime(true);
    include
dirname(__FILE__) . DIRECTORY_SEPARATOR . '../tableForShell.php';

   
$tableForShell = new TableForShell();
   
$tableForShell->setDelay(0.15);

   
$handle = fopen(dirname(__FILE__) . DIRECTORY_SEPARATOR . "csvData.csv", "r");
   
$row = 1;
    while ((
$data = fgetcsv($handle, 5000, ";")) !== FALSE) {
        if (
$row == 1) {
           
/* first line, header */
           
$fields = array();
           
$widths = array(5, 9, 9, 9, 30, 30, 30);
            for (
$c = 0; $c < count($data); $c++) {
               
$fields[] = array($data[$c], $widths[$c], 'C');
            }

           
$tableForShell->header($fields);
        }
        else {
           
/* lines */
           
for ($c = 0; $c < count($data); $c++) {
               
$text = $data[$c];
               
$width = $widths[$c];
               
$align = (is_numeric($text) ? 'R' : 'L');
               
$tableForShell->cell($text, $width, $align);
            }
           
$tableForShell->newline();
        }
       
$row++;
    }
   
fclose($handle);

   
$footerWidth = array_sum($widths) + count($widths) - 1;
   
$time = round(microtime(true) - $time, 2);
   
$tableForShell->footer(
        array(
            array(
$row . ' lines in ' . $time . ' secs.', $footerWidth - 31, 'C'),
            array(
'END OF PROC.', 30, 'C')
        )
    );