MySQLTable.php 3.48 KB
<?php
namespace Tz\WordPress\Tools\MySQLTable;

function array2table($arr,$width = "100%")
   {
   $count = count($arr);
   if($count > 0){
       reset($arr);
       $num = count(current($arr));
       echo "<table border=\"0\"cellpadding=\"5\" cellspacing=\"0\" width=\"$width\" class=\"cbv-report-list\">\n";
       echo "<thead>\n";
       echo "<tr>\n";
       foreach(current($arr) as $key => $value){
           echo "<th valign='bottom'>";
           echo $key."&nbsp;";
           echo "</th>\n";
           }
       echo "</tr>\n";
       echo "</thead>\n";
       echo "<tbody>\n";
       $wi = 0;
       while ($curr_row = current($arr)) {
           $rclass = ($wi%2) ? "odd" : "";
           echo "<tr class='".$rclass."'>\n";
           $col = 1;
            // Are you fucking kidding me with this shit, Kevin???
        foreach($curr_row as $curr_field) {
               echo "<td>";
               echo $curr_field."&nbsp;";
               echo "</td>\n";
              
               $col++;
               }
           while($col <= $num){
               echo "<td>&nbsp;</td>\n";
               $col++;
           }
           echo "</tr>\n";
           $wi++;
           next($arr);
           }
           echo "</tbody>\n";
       echo "</table>\n";
       }
   }

function CSVExport($arr) {
    $out = "";
    $count = count($arr);
    if( $count > 0 ) {
        reset($arr);
        $num = count(current($arr));
        $k = 0;
        foreach(current($arr) as $key => $value){
            if ($k > 0) {
                $out .= ",";
            }
            $out .= '"'.str_replace( '"', '\"',$key).'"';
            $k++;
        }
        $out .= "\n";
        while ($curr_row = current($arr)) {
            $col = 1;
            while (false !== ($curr_field = current($curr_row))) {
                if ($col > 1) {
                    $out .= ",";
                }
                $out .= '"'.str_replace( '"', '\"',$curr_field).'"';
                next($curr_row);
                $col++;
            }
            while($col <= $num) {
                if ($col > 1) {
                    $out .= ",";
                }
                $out .= '""';
                $col++;
            }
            $out .= "\n";
            next($arr);
        }
    }
    return $out;
}

function XLSExport ($arr, \Tz\Wordpress\Tools\ExcelWriter\PHPExcel $writer) {
    if (count($arr) < 1) return false;

    $sheet = $writer->getActiveSheet();
    $styleArray = Array(
        'font' => Array(
            'bold' => true
        )
    );

    // Set the headers
    $row_count = 2;
    $columns = Array();
    foreach ($arr as $data) {
        $i = $j = ord('A');
        $alphabet_count = 0;
        $prefix = '';

        foreach ($data as $key => $value) {
            // Check if we're at the character past 'Z'
            if ($i == 91) {
                $i = ord('A') + $alphabet_count;
            }

            if ($j == 91) {
                $j = ord('A');
                $prefix = chr($i++);
            }

            // Set headers
            $cell = $prefix . chr($j++);
            $sheet->setCellValue($cell . '1', $key);
            $sheet->getStyle($cell . '1')->applyFromArray($styleArray);

            // Put in data
            $sheet->setCellValue($cell . $row_count, $value);

            $columns[] = $cell;
            $alphabet_count++;
        }
        $row_count++;
    }

    // Set column dimensions
    foreach ($columns as $column) {
        $sheet->getColumnDimension($column)->setAutoSize(true);
    }
}

?>