<?php
/***************************************************************************
* MySQLDataViewer: a class to quickly display the result of a MySQL query
* as a table
*
* Written by Guido Pietrella <gp@pietrellanet.it>
* This software is released for Public Domain under the GPL License
*
* Version: 1.0 - June 30 2001
***************************************************************************/
class MySQLDataViewer {
/* events */
var $fOnEmptySet; // Occurs when there are no records to print out
var $fOnCheckData; // Occurs when a new record is fetched from the result set
var $fOnTitlePrint; // Occurs before printing the title of a column
var $fOnCellParams; // Occurs before printing the <td> tag to put special params in it
var $fOnDataPrint; // Occurs just before printing data
/* internal data */
var $aColumns; // Columns definition array
var $nMaxRow; // Maximum number of rows to display
var $bBrowserSafe; // Whether to convert HTML tags to text or not
var $oResultSet; // Query result to be processed;
var $aColumnsTitle; // Columns titles array
/* bacgrounds and params */
var $sTitleBgColor; // Background color for columns title
var $sRow1BgColor; // Background color for odd rows
var $sRow2BgColor; // Background color for even rows
var $sTableParams; // Parameters for the <table> tag (eg. align='center' etc...)
var $sCellParams; // Default parameters for the <td> tag (eg. align='center' etc...)
/* fonts */
var $sTitleFontTagOpen; // Opening font tag for columns title
var $sTitleFontTagClose;// Closing font tag for columns title
var $sDataFontTagOpen; // Opening font tag for cells
var $sDataFontTagClose; // Closing font tag for cells
/***************/
/* CONSTRUCTOR */
/***************/
function MySQLDataViewer($BrowserSafe = true)
{
$this->bBrowserSafe = $BrowserSafe;
$this->oResultSet = "";
$this->nMaxRow = -1;
}
/******************************/
/* PRINTS OUT THE WHOLE TABLE */
/******************************/
function Display($Result, $NoTitle = false)
{
if($Result == "")
return(false);
$this->oResultSet = $Result;
/* is the set empty?? */
if(@mysql_num_rows($Result) <= 0)
{
if($this->fOnEmptySet != "")
{
$fCallback = $this->fOnEmptySet;
$fCallback();
}
return(true);
}
if(!@mysql_data_seek($Result, 0))
return(false);
/* prints out the <table> tag */
$this->DisplayTableHeader();
/* prints out the columns title */
if(!$NoTitle)
{
if(!$this->DisplayTitle())
return(false);
}
/* prints out the cells */
$this->DisplayData();
/* prints out the </table> tag*/
$this->DisplayTableFooter();
}
/*******************/
/* PRINTS OUT DATA */
/*******************/
function DisplayData()
{
if($this->oResultSet == "")
return(false);
if(!is_array($this->aColumns) || @count($this->aColumns) <= 0)
$this->GetColumnsFromFields();
$nRows = 0;
while($aRow = mysql_fetch_array($this->oResultSet))
{
if($this->nMaxRow > -1 && $nRows >= $this->nMaxRow)
break;
if($this->fOnCheckData != "")
{
$fCallback = $this->fOnCheckData;
if(!($fCallback($aRow, $nRows)))
continue;
}
print("\n<tr>");
$nCols = 0;
reset($this->aColumns);
while(list($k, $v) = each($this->aColumns))
{
if(!$this->DisplayCell($aRow, $k, $v, $nRows, $nCols))
return(false);
$nCols++;
}
print("</tr>");
$nRows++;
}
return(true);
}
/****************************/
/* PRINTS OUT THE DATA CELL */
/****************************/
function DisplayCell($aRow, $sField, $aColDef, $nRow, $nCol)
{
$sData = $aRow[$sField];
$bHtml = !$this->bBrowserSafe;
if($this->sCellParams != "")
$sParams = $this->sCellParams;
if($this->fOnCellParams != "")
{
$fCallback = $this->fOnCellParams;
$fCallback(&$sParams, $sData, $aRow, $sField, $nRow, $nCol);
}
$sParams = trim($sParams);
print("\n<td");
/* bgcolor definition */
if(!preg_match("~bgcolor=~i", $sParams) && $this->sRow1BgColor != "" && $this->sRow2BgColor != "" )
{
if($aColDef["BgColor"] != "")
printf(" bgcolor='%s' ", $aColDef["BgColor"]);
else
printf(" bgcolor='%s' ", (($nRow % 2) ? $this->sRow1BgColor : $this->sRow2BgColor));
}
/* align definition */
if(!preg_match("~align=~i", $sParams) && $aColDef["Align"] != "")
printf(" align='%s' ", $aColDef["Align"]);
/* width definition */
if(!preg_match("~width=~i", $sParams) && $aColDef["Width"] != "")
printf(" width='%s' ", $aColDef["Width"]);
if($sParams != "")
printf(" %s ", $sParams);
print(">");
if($this->sDataFontTagOpen != "")
print($this->sDataFontTagOpen);
if($this->fOnDataPrint != "")
{
$fCallBack = $this->fOnDataPrint;
$fCallBack(&$sData, $aRow, $sField, $nRow, $nCol, &$bHtml);
}
if(trim($sData) != "")
{
if($bHtml)
print(trim($sData));
else
print(nl2br(htmlentities(trim($sData))));
}
else
print(" ");
if($this->sDataFontTagClose != "")
print($this->sDataFontTagClose);
print("</td>");
return(true);
}
/********************************/
/* PRINTS OUT THE COLUMNS TITLE */
/********************************/
function DisplayTitle()
{
if($this->oResultSet == "")
return(false);
if(!is_array($this->aColumnsTitle) || @count($this->aColumnsTitle) <= 0)
$this->GetTitlesFromFields();
print("\n<tr>");
reset($this->aColumnsTitle);
$nCol = 0;
while(list($k, $v) = each($this->aColumnsTitle))
{
print("\n<td");
if($this->sTitleBgColor != "")
printf(" bgcolor='%s' ", $this->sTitleBgColor);
print(">");
if($this->sTitleFontTagOpen != "")
print($this->sTitleFontTagOpen);
if($this->bBrowserSafe)
$v = nl2br(htmlentities($v));
if($this->fOnTitlePrint != "")
{
$fCallback = $this->fOnTitlePrint;
$fCallback(&$v, $nCol++);
}
if(trim($v) != "")
print(trim($v));
else
print(" ");
if($this->sTitleFontTagClose != "")
print($this->sTitleFontTagClose);
print("</td>");
}
print("</tr>");
return(true);
}
/*******************************/
/* PRINTS OUT THE TABLE HEADER */
/*******************************/
function DisplayTableHeader()
{
print("<table");
if($this->sTableParams != "")
printf(" %s ", $this->sTableParams);
print(">");
return(true);
}
/*******************************/
/* PRINTS OUT THE TABLE FOOTER */
/*******************************/
function DisplayTableFooter()
{
print("\n</table>");
return(true);
}
/*********************/
/* SETS TITLE PARAMS */
/*********************/
function SetTitleParams($BgColor = "", $FontTagOpen = "", $FontTagClose = "")
{
/* set background color for title */
if($BgColor != "")
$this->sTitleBgColor = $BgColor;
/* set font tag open */
if($FontTagOpen != "")
$this->sTitleFontTagOpen = $FontTagOpen;
/* set font tag close */
if($FontTagClose != "")
$this->sTitleFontTagClose = $FontTagClose;
}
/*********************/
/* SETS DATA PARAMS */
/*********************/
function SetDataParams($BgColor1 = "", $BgColor2 = "", $FontTagOpen = "", $FontTagClose = "")
{
/* set background color for odd data rows */
if($BgColor1 != "")
$this->sRow1BgColor = $BgColor1;
/* set background color for even data rows */
if($BgColor2 != "")
$this->sRow2BgColor = $BgColor2;
/* set font tag open */
if($FontTagOpen != "")
$this->sDataFontTagOpen = $FontTagOpen;
/* set font tag close */
if($FontTagClose != "")
$this->sDataFontTagClose = $FontTagClose;
}
/*********************/
/* SETS TABLE PARAMS */
/*********************/
function SetTableParams($sParams = "")
{
if($sParams != "")
$this->sTableParams = $sParams;
}
/**********************************/
/* GETS COLUMNS TITLE FROM FIELDS */
/**********************************/
function GetTitlesFromFields()
{
if($this->oResultSet == "")
return(false);
$this->aColumnsTitle = array();
/* if there are specific cols to display */
if(is_array($this->aColumns) && count($this->aColumns) > 0)
{
reset($this->aColumns);
while(list($k, $v) = each($this->aColumns))
{
if(is_array($v))
{
if(isset($v["Title"]))
$this->aColumnsTitle[] = $v["Title"];
else
$this->aColumnsTitle[] = $k;
}
}
}
else
{
$nNumFields = @mysql_num_fields($this->oResultSet);
for($i = 0; $i < $nNumFields; $i++)
$this->aColumnsTitle[] = @mysql_field_name($this->oResultSet, $i);
}
return(true);
}
/************************************/
/* GETS COLUMNS TO SHOW FROM FIELDS */
/************************************/
function GetColumnsFromFields()
{
if($this->oResultSet == "")
return(false);
if(is_array($this->aColumns) && count($this->aColumns) > 0)
return(true);
$this->aColumns = array();
$nNumFields = @mysql_num_fields($this->oResultSet);
for($i = 0; $i < $nNumFields; $i++)
{
$sFieldName = @mysql_field_name($this->oResultSet, $i);
$this->aColumns[$sFieldName] = array();
}
return(true);
}
};
?> |