Código PHP:
<link href="displaytagstyle.css" rel="stylesheet" type="text/css" />
<?php
class Table_Style{
var $s_table = 'dataTable';
var $s_tr_odd = 'odd';
var $s_tr_even = 'even';
function setTable($style){
$this->s_table = $style;
}
function getTable(){
return $this->s_table;
}
function setTr_Odd($style){
$this->s_tr_odd = $style;
}
function getTr_Odd(){
return $this->s_tr_odd;
}
function setTr_Even($style){
$this->s_tr_even = $style;
}
function getTr_Even(){
return $this->s_tr_even;
}
}
class Table{
var $style;
var $type = false;
function __construct(){
$this->style = new Table_Style();
}
function begin_table(){
echo "<table class='".$this->style->getTable()."'>\n";
}
function end_table(){
echo "</table>\n";
}
function begin_body(){
echo "<tbody>\n";
}
function end_body(){
echo "</tbody>\n";
}
function begin_head(){
echo "<thead>\n";
}
function end_head(){
echo "</thead>\n";
}
function add_th($html){
echo "<th>".$html."</th>\n";
}
function begin_tr(){
if($this->type){
echo "<tr class='".$this->style->getTr_Odd()."'>\n";
}else{
echo "<tr class='".$this->style->getTr_Even()."'>\n";
}
$this->type=!$this->type;
}
function end_tr(){
echo "</tr>\n";
}
function begin_td(){
echo "<td>";
}
function end_td(){
echo "</td>\n";
}
function add_td($html){
echo "<td>".$html."</td>\n";
}
}
class Cell{
var $index;
var $name;
var $type;
var $value;
var $ids;
function setIndex($index){
$this->index = $index;
}
function setName($name){
$this->name = $name;
}
function setType($type){
$this->type = $type;
}
function setValue($value){
$this->value = $value;
}
function setIds($ids){
$this->ids = $ids;
}
function getIndex(){
return $this->index;
}
function getName(){
return $this->name;
}
function getType(){
return $this->type;
}
function getValue(){
return $this->value;
}
function getIds(){
return $this->ids;
}
}
class Grid {
var $connect;
var $result;
var $property;
var $title;
var $i=0;
var $data;
var $table;
function __construct($hostname,$user,$pass,$dbname){
$this->set_connect($hostname,$user,$pass,$dbname);
}
function set_connect($hostname,$user,$pass,$dbname){
$this->connect = mysql_connect($hostname,$user,$pass);
mysql_select_db($dbname);
}
function set_sql($sql){
$this->result = mysql_query($sql,$this->connect);
}
function set_col($property){
$this->property[$this->i] = $property;
$this->title[$this->i] = $property;
//--Seteando un celda
$cell = new Cell();
$cell->setIndex($this->i);
$cell->setType('data');
$cell->setName($property);
$this->data[$this->i] = $cell;
$this->i++;
}
function set_col_title($property,$title){
$this->property[$this->i] = $property;
$this->title[$this->i] = $title;
//--Seteando un celda
$cell = new Cell();
$cell->setIndex($this->i);
$cell->setType('data');
$cell->setName($property);
$this->data[$this->i] = $cell;
$this->i++;
}
function set_html_title($html,$id,$title){
$this->title[$this->i] = $title;
$cell = new Cell();
$cell->setIndex($this->i);
$cell->setType('html');
$cell->setName($property);
$cell->setValue($html);
if($id)
$cell->setIds($id);
$this->data[$this->i] = $cell;
$this->i++;
}
function display(){
$this->table = new Table();
$this->table->begin_table();
//----Cabecera
$this->table->begin_head();
if(count($this->title)==0){
$numhead = mysql_num_fields($this->result);
$desheader = array();
for($i=0;$i<$numhead;$i++){
$this->table->add_th(mysql_field_name($this->result,$i));
$desheader[$i] = mysql_field_name($this->result,$i);
}
$this->property = $desheader;
$this->i = $numhead;
$this->table->end_head();
}else{
$numhead = count($this->title);
for($i=0;$i<$numhead;$i++){
$this->table->add_th($this->title[$i]);
}
$this->table->end_head();
}
//--Fin Cabecera
//--Cuerpo
$this->table->begin_body();
while($row = mysql_fetch_array($this->result)){
$this->table->begin_tr();
if(count($this->data)>0){//---Si especificaron por lo menos una columna
for($i=0;$i<count($this->data);$i++){
if($this->data[$i]->getType()=='data'){
$this->table->add_td($row[$this->data[$i]->getName()]);
}else if($this->data[$i]->getType()=='html'){
$temp_var = $this->data[$i]->getIds();
for($f=0;$f<count($temp_var);$f++){
$temp_var[$f] = $row[$temp_var[$f]];
}
$this->table->begin_td();
$this->printf_array($this->data[$i]->getValue(),$temp_var);
$this->table->end_td();
}
}
}else{
for($i=0;$i<count($this->property);$i++){
$this->table->add_td($row[$this->property[$i]]);
}
}
$this->table->end_tr();
}
//--Cuerpo
$this->table->end_body();
$this->table->end_table();
$this->close_connection();
}
function printf_array($format, $arr) {
return call_user_func_array('printf', array_merge((array)$format, $arr));
}
function close_connection(){
mysql_close($this->connect);
}
}
$obj = new Grid("localhost","root","root","dbconacsde");
$obj->set_sql("select per_id,per_nombres,per_apellidos from tbl_persona");
$obj->set_html_title("<input type='checkbox' name='codigo' value='%s' >",array("per_id"),"codigo");
$obj->set_col_title("per_nombres","nombres");
$obj->set_col_title("per_apellidos","apellidos");
$obj->set_html_title("<input type='text' name='txt%s' value='%s' >",array("per_id","per_nombres"),"Modificar");
$obj->display();
?>
El resultado es el siguiente , bueno en codigo que genera:
Código HTML:
<link href="displaytagstyle.css" rel="stylesheet" type="text/css" /> <table class='dataTable'> <thead> <th>codigo</th> <th>nombres</th> <th>apellidos</th> <th>Modificar</th> </thead> <tbody> <tr class='even'> <td><input type='checkbox' name='codigo' value='1' ></td> <td>Jose</td> <td>Lopez Mamani</td> <td><input type='text' name='txt1' value='Jose' ></td> </tr> <tr class='odd'> <td><input type='checkbox' name='codigo' value='2' ></td> <td>Luis</td> <td>Martinez Peres</td> <td><input type='text' name='txt2' value='Luis' ></td> </tr> </tbody> </table>