Código PHP:
<?php
class MySQL
{
private $conexion;
private $consulta;
function __construct()
{
$this->conexion = mysql_pconnect('localhost', 'root', '7135052367' );
if(!$this->conexion) {
$this->error_mysql = 'Error en la conexion';
}
mysql_select_db( 'administracion', $this->conexion) or die ('No se pudo seleccionar la base de datos');
}
function Query($Query)
{
$this->consulta = mysql_query($Query, $this->conexion) or die (mysql_error());
if(!$this->consulta) {
exit();
}
}
function fecth_array()
{
return mysql_fetch_array($this->consulta);
}
function fetch_assoc()
{
return mysql_fetch_assoc($this->consulta);
}
function num_row()
{
return mysql_num_rows($this->consulta);
}
function ultimo_id()
{
return mysql_insert_id();
}
}
require_once'./seguridad.php';
$mysql = new MySQL();
class GnTabla extends MySQL
{
private $col = array(); //var who contain format table
public $auto_increment = 1; //by default
public $col_sum = array(); //sum of row type numeric, called by row name
public $class_status = 'on'; //by default, use('on') or don't('off') a class/style
public $class_func; //function name of a class/style (function;row_1,row_2,row_3, ...., row_n)
public $num_sep = ','; //by default, separator of thousands
public $num_dec = 2; //by default, number of decimal
public $head_cont = array(); //table var-system
public $foot_cont = array(); //table var-system
private $class_tbl, $align, $style; //style vars
private $num_row; //var system
function style()
{
$class = @func_get_arg(0);
$style = @func_get_arg(1);
$align = @func_get_arg(2);
$this->class_tbl = (!empty($class)) ? ' class="'.$class.'"' : NULL;
$this->style = (!empty($style)) ? ' style="'.$style.'"' : NULL;
$this->align = (!empty($align)) ? ' align="'.$align.'"' : NULL;
}
public function Type($Type)
{
//difined type
foreach($Type as $key => $value) {
$this->col[$key]['Type'] = $value;
}
}
public function Header($Key)
{
//difined Header
foreach(explode(",", $Key) as $key) {
$this->col[$key]['Header'] = true;
}
}
public function Format($Name)
{
//defined table-format
foreach($Name as $key => $value) {
$this->col[$key]['Name'] = $value;
}
}
private function PutOrderCol()
{
ksort($this->col); //put in order the array
}
public function TQuery($query)
{
if(!strpos($query, 'LIMIT')) {
$query .= ' LIMIT 500';
}
parent::__construct();
parent::Query($query);
$this->num_row = parent::num_row();
}
public function Display()
{
/*function que evalua las variables, interpreta los resultados de la base de datos, y finalmente la imprime */
if($this->num_row == 0) {
echo "<p>No se encontraron resultados</p>\n";
} else {
if($this->num_row == 500) {
echo '<div style="clear:left;" class="alerta">Los resultados se limitaron a 500</div>'."\n";
}
$this->CreateTable();
}
}
private function CreateTable()
{
$this->PutOrderCol();
echo "<table$this->class_tbl$this->style$this->align>\n";
$this->Head();
echo " <tbody>\n";
while($datos = parent::fetch_assoc()) {
if($this->class_status != 'off') {
$class = ' class="'; //start
if(empty($this->class_func)) {
$class .= $class_name = ($class_name == 'even') ? 'odd' : 'even';//even and odd have to be style defined
} else {
list( $name_fun, $row_fun) = explode(";", $this->class_func);
$c_parameter[] = $class_name;
foreach(explode(",", $row_fun) as $col_name) {
$c_parameter[] = $datos[$col_name];
}
$class .= $class_name = call_user_func_array($name_fun, $c_parameter);
unset($c_parameter);
}
$class .= '"'; //end
}
echo " <tr$class>\n";
foreach($this->col as $COL) {
echo ($COL['Header']) ? ' <th>' : ' <td>';
if($COL['Type'] == 'Text' or empty($COL['Type'])) {
echo $datos[$COL['Name']];
} elseif($COL['Type'] == 'pre_f') {
list( $search_replace, $text ) = explode("~", $COL['Name']);
foreach(explode(",", $search_replace) as $array) {
$str_replace[] = $datos[$array];
$str_search[] = '@'.$array;
}
echo str_replace($str_search, $str_replace, $text);
unset($str_replace);
} elseif($COL['Type'] == 'int') {
$this->col_sum[$COL['Name']] += $datos[$COL['Name']];
echo number_format($datos[$COL['Name']], 0, '.', $this->num_sep);
} elseif($COL['Type'] == 'float') {
$this->col_sum[$COL['Name']] += $datos[$COL['Name']];
echo number_format($datos[$COL['Name']], $this->num_dec, '.', $this->num_sep);
} elseif($COL['Type'] == 'auto_inc') {
echo $this->auto_increment++;
} else {
foreach(explode(",", $COL['Name']) as $f_parameters) {
$parameters[] = ($datos[$f_parameters] != '') ? $datos[$f_parameters] : $f_parameters;
//if row-name don't exists
}
call_user_func_array($COL['Type'], $parameters);
unset($parameters);
}
echo ($COL['Header']) ? "</th>\n" : "</td>\n";
}
echo " </tr>\n";
}
echo " </tbody>\n";
$this->Foot();
echo "</table>\n";
//turn by default
unset($this->col, $this->class_func, $this->class_tbl, $this->align, $this->style);
$this->auto_increment = 1;
$this->class_status = 'on';
$this->head_cont = array(); //table var-system
$this->foot_cont = array(); //table var-system
} //end function Display()
private function Head()
{
if(!empty($this->head_cont)) {
echo " <thead>\n <tr>\n";
foreach ($this->head_cont as $tmp) {
echo " <th>$tmp</th>\n";
}
echo " </tr>\n </thead>\n";
}
}
private function Foot()
{
if(!empty($this->foot_cont)) {
echo " <tfoot>\n";
foreach($this->foot_cont as $tmp) {
echo " <th>";
echo (empty($this->col_sum[$tmp])) ? $tmp : $this->col_sum[$tmp];
echo "</th>\n";
}
echo " </tfoot>\n";
}
}
public function caption($caption)
{
echo " <caption>$caption</caption>\n";
}
public function Col($col)
{
//formato de variable array( propiedad => array( valores))
echo " <colgroup>\n";
foreach($col as $clave => $valor) {
$n = 0;
foreach($valor as $tmp) {
if(!empty($tmp)) {
$fila_col[$n++] .= " $clave=\"$tmp\"";
} else {
$fila_col[$n++] .= '';
}
}
}
for($i = 0; $i<count($fila_col); $i++) {
echo " <col$fila_col[$i] />\n";
}
echo " </colgroup>\n";
}
}
?>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es">
<link href="/CSS/Tabla.css" rel="stylesheet" type="text/css" />
<head>
</head>
<body>
<?php
function ClassArticulo($class, $estado, $uni, $min) {
if ($estado == 'Off') {
return 'cancelado';
} elseif ($uni == 0) {
return 'inexistente';
} elseif ($uni <= $min) {
return 'proceso';
} elseif ($class == 'even') {
return 'odd';
} else {
return 'even';
}
}
//ejemplo #1
$tabla = new GnTabla();
$tabla->style('datos');
$tabla->head_cont = array('','Nombre Barra','Nombre','Unidades','P. Venta','P Compra','Min Existencias','stado');
$tabla->Type(array(0 => 'auto_inc', 2 => 'pre_f', 4 => 'float', 5 => 'float', 6 => 'float'));
$tabla->Header('2');
$tabla->class_func = 'ClassArticulo;estado_articulo,unidades,minimo_existencias';
$tabla->Format(array(
'',
'codigo_barra',
'IdArticulo,nombre_articulo~<a href="/administracion/ventas_admin.php?IdArticulo=@IdArticulo">@nombre_articulo</a>',
'unidades',
'minimo_existencias',
'precio_venta',
'precio_compra',
'estado_articulo'));
$tabla->TQuery('SELECT `IdArticulo`, `codigo_barra`, `nombre_articulo`, `unidades`, `minimo_existencias`, `precio_venta`, `precio_compra`, `estado_articulo` FROM inventario order by IdArticulo DESC');
$tabla->Display();
echo "<br>\n";
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//ejemplo #2
$GLOBALS['mysql']->Query("SELECT `IdCompania`, `IdServicio` FROM companias_servicios");
while($datos = $GLOBALS['mysql']->fecth_array()) {
$company[$datos['IdCompania']][$datos['IdServicio']] = true;
}
function SeeServ($IdCompany, $s) {
echo ($GLOBALS['company'][$IdCompany][$s]) ? 'Si' : 'No';
}
$tabla->style('datos');
$tabla->head_cont = array(
'',
'Nombre',
'Envio de dinero',
'Entrega de dinero',
'Pago de bill',
'Money order',
'Envio de paqueteria',
'Recargas electronicas',
'Estado');
$tabla->Type(array(
0 => 'auto_inc',
1 => 'pre_f',
2 => 'SeeServ',
3 => 'SeeServ',
4 => 'SeeServ',
5 => 'SeeServ',
6 => 'SeeServ',
7 => 'SeeServ'));
$tabla->Header('1');
$tabla->Format(array(
'',
'IdCompania,nick~<a href="/administracion/companias.php?IdCompania=@IdCompania">@nick</a>',
'IdCompania,1',
'IdCompania,2',
'IdCompania,3',
'IdCompania,4',
'IdCompania,5',
'IdCompania,6',
'estado'));
$tabla->TQuery('SELECT `IdCompania`, `nick`, `estado` FROM companias');
$tabla->Display();
?>
</body>
</html>