<?php
/***
Clase: MyDataText
Autor: Horacio M. Herrera
Fecha: 20/04/2007
Propósitpo: Generar datos en formato texto plano y enviarlos directamente al navegador
Versión final para publicar
Explanation This class can be used to send records selected from a table connected by ODBC,
directly to a browser, like a CSV file
*/
class MyDataText
{
var $SqlString; // SQL a ejecutar / SQL to execute
var $IsError; // 1 Hay un problema / There is a problem !!!
var $ErrorString; // If there is an error, this string explains it
var $DB; // Database Name
var $DBhost; // Database Host
var $DBuser; // DataBase User
var $DBpassord; // DataBase Password
var $DSN; // ODBC - DSN
var $nomBloque; // Nombre del bloque de datos / Block name
var $cantrec; // Cantidad de registros del resultado / Qtty of records
var $def_sep; // Delimitador de campos / Field separator
var $def_CR; // Salto de línea / Carriage return
function MyDataText()
{
/***
Inicializa los valores por defecto / Initialize the values with DEFAULT value
*/
$this->cantrec = 0;
$this->IsError=0;
$this->ErrorString="NO errors ;)";
$this->SqlString="";
$this->def_CR = "<br/>";
if (!isset($this->def_sep)) {
$this->def_sep = ";";
}
}
function MakeConnection ()
{
/***
Establece la conexión a la base de datos / Make a connection to Database Server vía ODBC
*/
$link = odbc_connect ($this->DSN, $this->DBuser, $this->DBpassword) or die ("I can't connect with DataBase :(");
return $link;
}
function DoText()
{
/***
Esta es la función principal / This is the main function
Genera los resultados / It generates de result
*/
if ($this->SqlString=="")
{
// Si no se estableció el query / If it is not set query string
$this->IsError=1;
$this->ErrorString="Error in Sql Query !!!";
return -1;
}
if (!$Ob_Conn=$this->MakeConnection())
{
// No hay conexión / There is not connection
exit(1);
}
// Ejecutemos el query / Lets execute the query
$result = odbc_exec($Ob_Conn, $this->SqlString);
if ($result==FALSE)
{
$errmsg = "";
$errnro = "";
if ($errno = odbc_error($Ob_Conn))
{
$errmsg = odbc_errormsg($Ob_Conn);
}
$this->IsError=1;
$this->ErrorString="Error in SQL Query : ".$this->SqlString . "Mensaje: (".$errnro.") - " . $errmsg;
return -1;
}
$i = 0;
$FieldsVector=array(); // Cabecera con nombres de campo / String that contens fieldnames
$cant_campos = odbc_num_fields ($result);
// Primer fila con los nombres de campos / First row containing fieldnames
echo $this->nomBloque.$this->def_sep;
for ($i=1; $i <= $cant_campos; $i++)
{
$fn=odbc_field_name($result,$i);
if ($fn == "") {
$fn = "expr".$i;
}
echo $fn.$this->def_sep;
$FieldsVector[]=$fn;
}
echo $this->def_CR;
// Retornar datos / Return data
$row=array();
$k = 1;
while ($row = odbc_fetch_row ($result, $k))
{
$k++;
echo $this->nomBloque.$this->def_sep;
for ($j=0; $j<$cant_campos; $j++)
{
$FieldName="";
$Attributes="";
$fdata = odbc_result($result,$j+1);
echo htmlspecialchars($fdata).$this->def_sep;
}
echo $this->def_CR;
}
$this->cantrec = $k-1;
odbc_free_result($result);
} // End of Function : DoText
function odbc_fetch_object($result)
{
$rs=array();
if(odbc_fetch_into($result,&$rs))
{
foreach($rs as $key=>$value)
{
$fkey=strtoupper(odbc_field_name($result,$key+1));
$rs_obj->$fkey = trim($value);
}
}
return($rs_obj);
}
} // End of Class XML
?>