Welcome Guest, Not a member yet? Register   Sign In
render query database to csv, html table and xml
#1

[eluser]Unknown[/eluser]
Sorry for my bad English.

this library has the function of generating xml, csv and html table from a query in the database.

Code:
<?php

/**
* Biblioteca responsável por gerar uma tabela com os resultados do banco de dados.
* @author Carlos Lorenzon
*/
class DataRender {

    private $query;

    /**
     * Method returns the result of a query in an html table.<br />
     * Customize the look by adding to your CSS class name <b>datatable</b>.
     * @param query $query query database
     * @param array $tableData column names from the database
     * @param array $tableHeader column names header to data Table
     * @param string $class class table html
     * @param string $trParam parameters to TR html table (ex: javascript, id)
     * @param string $tdParam parameters to TD html table (ex: javascript, id)
     * @return string Table with the results
     */
    public function dataTable($query, $tableData, $tableHeader = 0, $class="datatable", $trParam="", $tdParam="") {
        
        
        if ($query->num_rows() > 0) {
            $CI = & get_instance();
            $CI->load->helper('string');

            $retorno = '<table class="'.$class.'"><tr>';
            if (($tableHeader != 0) and (count($tableData) == count($tableHeader))) {

                foreach ($tableHeader as $th) {
                    $retorno.= "<th>$th</th>";
                }
            } else {

                foreach ($tableData as $th) {
                    $retorno.= "<th>$th</th>";
                }
            }

            $retorno.= "</tr>";

            foreach ($query->result() as $row) {
                $classTd = alternator('class="odd"', 'class="even"');
                $retorno.= "<tr $trParam>";
                foreach ($tableData as $td) {
                    $retorno.= "<td $classTd $tdParam>" . $row->$td . "</td>";
                }
                $retorno.= "</tr>";
            }
            $retorno.="</table>";
        } else {
            $retorno = "Não existem registros a serem apresentados";
        }
        return $retorno;
    }

    /**
     * Method returns the result of a query in an xml string.<br />
     * @param query $query query database
     * @param string $root root element xml
     * @param string $element element xml
     * @return string xml with the results
     */
    public function xml($query, $root="root",$element="element") {

        if ($query->num_rows() > 0) {
            $CI = & get_instance();
            $CI->load->dbutil();
            
            $config = array (
                  'root'    => $root,
                  'element' => $element,
                  'newline' => "\n",
                  'tab'    => "\t"
                );

            $xml = $CI->dbutil->xml_from_result($query, $config);

            return $xml;
        }
    }
    
    /**
     * Method returns the result of a query in an csv string.<br />
     * @param query $query query database
     * @param array $tableData column names from the database
     * @param string $separetor separator csv
     * @return string csv with the results
     */
    public function csv($query, $tableData, $separator=";") {

        if ($query->num_rows() > 0) {
            
            $retorno = "";
            foreach ($query->result() as $row) {
                
                foreach ($tableData as $td) {
                    $retorno.= str_replace($separator ,'', $row->$td ) . $separator;
                }
                $retorno.= "\n";
            }
            
        }
        return $retorno;
    }

}

?&gt;

Method of use:
Code:
$this->load->library('datarender');      
        $query = $this->db->get('table-name');
        
        $dataRender = new DataRender();
        //to html table
        $conteudo = $dataRender->dataTable($query, array('column-name-DB', 'column-name-DB'), array('column-name-Table','column-name-Table'));
        //If you do not fill in the name of the columns for the html pick the name of the database columns
        
        // to xml
        $xml = $dataRender->xml($query, "Configuracoes", "Configuracao");
        
        //to csv
        $csv = $dataRender->csv($query,array('value'));

Hope you enjoy and look forward suggestions
#2

[eluser]Unknown[/eluser]
Nice Post dude, keep it up




Theme © iAndrew 2016 - Forum software by © MyBB