Generate dynamic data from query including headers

Below code can be used to generate a data listing including dynamic headers .



File1: CLASS.database.php

<?php
// Class : begin 
 class Database
 {
 var $host;     
 var $password;
 var $user;    
 var $database;
 var $link;
 var $query;
 var $result;
 var $rows;

 function Database()
 { // Method : begin


  $this->host = "";   
  $this->password = "";
  $this->user = "";
  $this->database = "";
 

 
  $this->rows = 0;

 } // Method : end

 function OpenLink()
 { // Method : begin
  $this->link = @mysql_connect($this->host,$this->user,$this->password) or die (print "Class Database: Error while connecting to DB (link)");
 } // Method : end

 function SelectDB()
 { // Method : begin

 @mysql_select_db($this->database,$this->link) or die (print "Class Database: Error while selecting DB");
 
 } // Method : end

 function CloseDB()
 { // Method : begin
 mysql_close();
 } // Method : end

 function Query($query)
 { // Method : begin
 $this->OpenLink();
 $this->SelectDB();
 $this->query = $query;
 $this->result = mysql_query($query,$this->link) or die (print "Class Database: Error while executing Query :: $query.");

// $rows=mysql_affected_rows();

if(ereg("SELECT",$query))
{
 $this->rows = mysql_num_rows($this->result);
}

 $this->CloseDB();
 } // Method : end   
 

function Query1($query)
 { // Method : begin
 $this->OpenLink();
 $this->SelectDB();
 $this->query = $query;
 $this->result = mysql_query($query,$this->link) or die (print "Class Database: Error while executing Query :: $query.");

return  $this->result;
 } // Method : end     
 

 
 }

 // Class : end

?>


File2: generateReport.php

<?php
require_once("class.database.php");

$database=new database();

$sql="select * from employee";

$result = $database->Query1($sql);


$numfields = mysql_num_fields($result);
  

    while ($fielddata = mysql_fetch_array($result)) {
     echo '<table>';
     echo '<tr>';
    for ($i = 0; $i<$numfields; $i += 1) {
        $field = mysql_fetch_field($result, $i);
        echo '<th>' . $field->name . '</th>';
    }
    echo '</tr>';
   
        echo '<tr>';
        for ($i = 0; $i<$numfields; $i += 1) {
            $field = mysql_fetch_field($result, $i);
            if($fielddata[$field->name] != "")
            {
             echo '<td>' . $fielddata[$field->name] . '</td>';
            }       
            else
            {
             echo '<td>&nbsp;</td>';
            }
          
        }
        echo '</tr>';
        echo '</table>';
    }
?>

No comments:

Post a Comment