Location: PHPKode > scripts > SDReport > sdreport/class.SDReport.php
<?php

/*
**********************************************************************
*
*	SDReport - Report Class Generator
*	
*	version 1.0
*	Release Date: 19/December/2007
*	
*	Author: Silvio Delgado
*	Website: www.silviodelgado.com.br
*	Email: hide@address.com
*	
*	License:
*	This is a free software, protected by GPL 
*	(www.gnu.org/copyleft/gpl.html), from Free Software Foundation
*	This software may be redistributed only with full source code.
*	Any modifications must be join to the rest of code 
*	and submitted to the original author.
*	
*	If you modify any file of this class, 
*	you must mantain all of this credits.
*	
*	Lastest releases are available from the author website, 
*	at www.silviodelgado.com.br
*
**********************************************************************
*/

include('iSDReport.php');
include('./lang/' . $language . '.php');

class SDReport implements iSDReport
{
	protected $numCols;
	protected $numRows;
	protected $cols = array();
	protected $header;
	protected $item;
	protected $footer;
	protected $showHeader;
	protected $showFooter;
	protected $headerStyle;
	protected $itemStyle;
	protected $footerStyle;
	
	protected $title;
	protected $tableWidth;
	protected $copyright;
	protected $prepared = 0;
	
	protected $report;

	protected $dbData;

	public function __construct($dbConfig)
	{
		$this->dbData = $dbConfig;
	}
	
	function __call($method, $params)
	{
		if ($method == "Show")
		{
			if ($this->prepared)
				echo $this->report;
			else
				echo _NAOPREPARADO;
		}
	}
	
	public function Title($value)
	{
		$this->title = $value;
	}
	
	public function ShowHeader($value)
	{
		$this->showHeader = $value;
	}
	public function ShowFooter($value)
	{
		$this->showFooter = $value;
	}	
	public function HeaderStyle($style)
	{
		$this->headerStyle = $style;
	}
	public function ItemStyle($style)
	{
		$this->itemStyle = $style;
	}
	public function FooterStyle($style)
	{
		$this->footerStyle = $style;
	}
	public function TableWidth($value)
	{
		$this->tableWidth = $value;
	}
	public function Columns($arrCol)
	{
		//$this->numCols = count($arrCol);
		$this->cols = $arrCol;
	}

	protected function GenHeader()
	{
		$this->header = "<tr>";
		for ($i = 0; $i < count($this->cols); $i++)
		{
			$this->header .= "<td";
			if ($this->headerStyle)
			{
				$this->header .= " class=\"" . $this->headerStyle . "\"";
			}
			$this->header .= ">" . $this->cols[$i] . "</td>";
		}
		$this->header .= "</tr>";
	}

	protected function GenFooter()
	{
		$this->footer = "<tr><td";
		if (isset($this->footerStyle))
			$this->footer .= " class=\"" . $this->footerStyle . "\"";
		if (count($this->cols) > 0)
			$this->footer .= " colspan=\"" . count($this->cols) ."\"";
		$this->footer .= ">" . _NUMREGS . " " . $this->numRows . "</td></tr>";
	}
	
	protected function GenCopyright()
	{
		$this->copyright .= "<tr><td colspan=\"" . count($this->cols) ."\" "
			."style=\"text-align:right\"><a href=\"http://www.silviodelgado.com.br\" target=\"_blank\">"
			."<span style=\"font-family:Arial,Verdana;font-size:9px\">"
			. _COPYRIGHT . " SDReport</span></a></td></tr>";
	}

	protected function Prepare()
	{
		$this->prepared = 1;
		$this->GenCopyright();

		if (isset($this->title))
			$this->report = "<h1>:: " . $this->title . " ::</h1>";

		return $this->prepared;
	}
	
	protected function Mount()
	{
		if (count($this->cols) > 0)
		{
			if (isset($this->showHeader) && ($this->showHeader == 1))
			{
				$this->GenHeader();
			}
			if (isset($this->showFooter) && ($this->showFooter == 1))
			{
				$this->GenFooter();
			}
		}
	}
	public function Generate($sql)
	{
		if ($this->Prepare())
		{
			$conn = @mysql_connect($this->dbData[0], $this->dbData[1], $this->dbData[2]) or die (_CONNERRO);
			@mysql_select_db($this->dbData[3], $conn);
		
			$this->item = "";
			
			$result = @mysql_query($sql, $conn) or die(); // print_r($result);
			if (@mysql_num_rows($result) > 0)
			{
				$this->numCols = mysql_num_fields($result);
				$this->numRows = 0;
				while ($data = @mysql_fetch_array($result, MYSQL_BOTH))
				{
					$this->item .= "<tr>";
					for ($i = 0; $i < $this->numCols; $i++)
					{
						$this->item .= "<td";
						if (isset($this->itemStyle))
						{
							$this->item .= " class=\"" . $this->itemStyle . "\"";
						}
						$this->item .= ">" . $data[$i] . "</td>";
					}
					$this->item .= "</tr>";
					$this->numRows++;
				}
				@mysql_free_result($result);
			}
			else
			{
				$this->item .= "<td";
				if (isset($this->itemStyle))
				{
					$this->item .= " class=\"" . $this->itemStyle . "\"";
				}
				if (count($this->col) > 0)
					$this->item .= " colspan=\"" . count($this->cols) . "\"";
				$this->item .= ">" . _SEMREGISTROS . "</td>";
			}

			$this->Mount();
			if (isset($this->tableWidth))
			{
				$this->report .= "<table width=\"" . $this->tableWidth . "\">";
			}
			else
			{
				$this->report .= "<table>";
			}
			$this->report .= $this->header;
			$this->report .= $this->item;
			$this->report .= $this->footer;
			$this->report .= $this->copyright;
		
			$this->report .= "</table>";
		}
		else
		{
			$this->report = _NAOPREPARADO;
		}
	}
	public function GetNumPages()
	{
		return 0;
	}
	public function Export($format)
	{
		return "";
	}
}

?>
Return current item: SDReport