Sådan benyttes komponenten DataReader klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/DataReader.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? DataReader::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new DataReader($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten DataReader klassen
numFields=0
numrows=0
Den fulde PHP kildekode for DataReader klassen
<?php/** * @package dto * @see HTML_DTO_UTIL_PATH.'/DataReader.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_DTO_UTIL_PATH.'/HelperDataReader.php');require_once(HTML_BASIC_UTIL_PATH.'/Message.php');if (defined('HTML_LOG_UTIL_PATH')) { require_once(HTML_LOG_UTIL_PATH.'/Log.php');}/** * The Data Transfer Object used as a data carrier between systems. * The Data Reader towards the mysql database * and the rendering of the ViewXyz table/form components * <code> * Usage: * $datareader = new DataReader(); * // fill data into the datareader object from a call to mysql * * // Print the result * print $datareader->getNumRows(); * print $datareader->getFieldName($name); * </code> * @package dto */class DataReader { /** * @var int $numRows The number of rows returned in the query */ protected $numRows = ''; /** * @var boolean $rc The return code returned in the query */ protected $rc = ''; /** * @var array $rows The array of rows returned in the query */ protected $rows = array(); // of rows /** * @var int $numFields The number of fields returned in the query */ protected $numFields = ''; /** * @var array $fieldName The array of names for each field */ protected $fieldName = array(); /** * @var array $fieldLen The array of length for each field */ protected $fieldLen = array(); /** * @var array $fieldType The array of types for each field */ protected $fieldType = array(); /** * @var array $fieldFlags The array of field flags for each field */ protected $fieldFlags = array(); /** * @var array $fieldTable The array of table names for each field */ protected $fieldTable = array(); /** * @var boolean $sort The datareader may be sorted if true */ protected $sort = ''; /** * @var String $msg The message about the dto */ protected $msg = ''; /** * Constructor */ function __construct() { $this->numRows = 0; $this->numFields = 0; $this->rc = false; $this->sort = false; } /** * Returns the name of the instanciated class * @return String The name of the class */ function getClassName() { return __CLASS__; } /** * Set the named attribute (key) of a class to specified value * Display an error message and exit, If not a known attribute * i.e. You MUST define the attribute at class scope as ... protected $myattr=''; * @param String $key The key to set with the value specified * @param String $value The value of the key */ function set($key, $value) { if (array_key_exists($key,get_class_vars(get_class($this)))) { $this->$key = $value; } else { $msg = $this->getClassName()."->set() not a valid key<br />\r\n"; $msg .= "Format: class->key=value<br />\r\n "; $msg .= 'Found : '.$this->getClassName().'->'.$key.'='.(isset($value)?''.$value:gettype($value)); $this->error($msg, __FILE__, __LINE__); } } /** * Get the complete html for a key * Display an error message and exit, If not a known attribute * i.e. You MUST define the 'key' at class scope as ... protected $mykey=''; * @param String $key The value to return for the specified index * @param String $index The index to use, if an array * @param String $default The default value to use if no match * @return String the html for the attribute (key) */ function get($key, $index='', $default='') { $html = ''; $vars = get_object_vars($this); if (array_key_exists($key,get_class_vars(get_class($this))) || array_key_exists($key, $vars)) { if ($index != '' && is_array($this->$key)) { if (array_key_exists($index, $this->$key)) { $html = $this->{$key}[$index]; } else { if (count($this->$key) > 0) { $msg = $this->getClassName()."->get('$index') not a valid key<br />\r\n"; $msg .= "Format: rc = class->key[index]<br />\r\n "; $msg .= 'Found : '.$this->getClassName().'->'.$key.'['.(isset($index)?''.$index:gettype($index)).']'.' this->key='.$this->$key.' default='.$default; if (is_array($this->$key)) { $msg .= "<br />\r\narray:".count($this->$key);; foreach($this->$key as $key=>$value) { $msg .= "$key=>$value<br />\r\n"; } } $this->error($msg, __FILE__, __LINE__); } else { // OK, ignore } if (array_key_exists($index, $this->{$key})) { $html = $this->{$key}[$index]; } else { //die('DataReader.'.$html."- $key $index"); } } } else { $html = $this->$key; } } else { // Ignore } if ($html=='') { $html .= $default; } return $html; } /** * Add a row * @param String $key The key to use in the array * @param Object $value The value to add to array * @param String $name The name to use in the class */ function add($key, $value, $name=DATA_READER_ROWS) { $this->{$name}[$key]=$value; } /** * Return the array of rows * @param int $index The index to use * @return array The array of rows */ function getRows($index='') { return $this->get(DATA_READER_ROWS, $index); } /** * Print or log error messages * @param String $msg The message * @param String $file The file name * @param String $line The line number */ function error($msg, $file, $line) { $this->setMsg("$msg file=$file in line=$line"); if (defined('HTML_LOG_UTIL_PATH')) { Log::debug($msg, $file, $line); } else { Message::add($msg, __FILE__, __LINE__); } } /** * Helper functions */ function getSort() { return $this->get(DATA_READER_SORT); } function setSort($value) { if ($value !== '' && $value !== true && $value !== false) { $msg = $this->getClassName()."->setSort('$value') not true or false<br />\r\n"; if (defined('HTML_LOG_UTIL_PATH')) { Log::fatal($msg, __FILE__, __LINE__); } Message::add($msg, __FILE__, __LINE__); } $this->set(DATA_READER_SORT, $value); } function getRc() { return $this->get(DATA_READER_RC); } function setRc($value) { $this->set(DATA_READER_RC, $value); } /** * Bugfix, when testing */ function getNumRows() { return $this->get(DATA_READER_NUM_ROWS); } function setNumRows($value) { $this->set(DATA_READER_NUM_ROWS, $value); } function getNumFields() { return $this->get(DATA_READER_NUM_FIELDS); } function setNumFields($value) { $this->set(DATA_READER_NUM_FIELDS, $value); } /** * Get the FieldName, FieldLen, FieldType, FieldFlags, FieldTable * @param int $index The index to get the value for * @return String The requested value */ function getFieldName($index='') { return $this->get(DATA_READER_FIELD_NAME, $index); } function getFieldLen($index='') { return $this->get(DATA_READER_FIELD_LEN, $index); } function getFieldType($index='') { return $this->get(DATA_READER_FIELD_TYPE, $index); } function getFieldFlags($index='') { return $this->get(DATA_READER_FIELD_FLAGS, $index); } function getFieldTable($index='') { return $this->get(DATA_READER_FIELD_TABLE, $index); } /** * Add the FieldName, FieldLen, FieldType, FieldFlags, FieldTable * @param String $key The key to use * @param String $value The value to add for the key */ function addFieldName($key, $value) { $this->add($key, $value,DATA_READER_FIELD_NAME); } function addFieldLen($key, $value) { $this->add($key, $value,DATA_READER_FIELD_LEN); } function addFieldType($key, $value) { $this->add($key, $value,DATA_READER_FIELD_TYPE); } function addFieldFlags($key, $value) { $this->add($key, $value,DATA_READER_FIELD_FLAGS); } function addFieldTable($key, $value) { $this->add($key, $value,DATA_READER_FIELD_TABLE); } /** * Set the message * @param String $msg The message */ function setMsg($msg) { $this->msg = $msg; } /** * Get the message * @return String The message */ function getMsg() { return $this->msg; } /** * Get the html code * @return String The html code */ function getHtml() { $html = ""; $helper = new HelperDataReader($this); $html .= $helper->getHtml(); return $html; }}?>
Den fulde HTML kildekode for DataReader klassen
<? <p>numFields=0</p> <table border="1" cellpadding="0" cellspacing="2"> <tr> <th>Name</th> <th>Len</th> <th>Type</th> <th>Flags</th> <th>Table</th> </tr> </table> <p>numrows=0</p> ?>
Her er 'klasse metoderne' for DataReader klassen:
Her er 'objekt variable' for DataReader klassen: