Sådan benyttes komponenten ViewLimit klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/ViewLimit.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? ViewLimit::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new ViewLimit($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten ViewLimit klassen
Den fulde PHP kildekode for ViewLimit klassen
<?php/** * @package mvc * @see HTML_MVC_VIEW_PATH.'/ViewLimit.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 */// todo <span class="TODO"/** * The required files */require_once(HTML_TABLE_COMPONENT_PATH.'/TableHeader.php');require_once(HTML_TABLE_COMPONENT_PATH.'/Table.php');require_once(HTML_TABLE_COMPONENT_PATH.'/Tr.php');require_once(HTML_TABLE_COMPONENT_PATH.'/Td.php');require_once(HTML_BASE_UTIL_PATH.'/Link.php');require_once(HTML_UTIL_COMPONENT_PATH.'/Server.php');require_once(HTML_UTIL_COMPONENT_PATH.'/Request.php');require_once(HTML_UTIL_COMPONENT_PATH.'/Params.php');if (defined('HTML_BASIC_UTIL_PATH')) { require_once(HTML_BASIC_UTIL_PATH.'/Message.php');}/** * Returns a view of the << < 1 2 3 > >> utilizing the mysql LIMIT x,y * <code> * Usage: * $datareader = null; * $text = The count for the query; * $width = "100%"; * $class = "theTable"; * $border = "1"; * $cellpadding = "5" * $cellspacing = "0"; * $summary = ""; * $caption = ""; * $id = ""; * * $rows = array( array(), array(), ); * $text = count($rows); * $view = new ViewLimit($datareader, $text, $width, $class, $border, $cellpadding, $cellspacing, $summary, $caption, $id); * print $view->getHtml(); * Or * ViewLimit::display($datareader, $text, $width, $class, $border, $cellpadding, $cellspacing, $summary, $caption, $id) * </code> * @package mvc */class ViewLimit extends Table { /** * @var int $numRows The number of rows in the query */ protected $numRows = ''; /** * @var String $href The url to go next */ protected $href = ''; /** * @var String $table The table name to use, when moving around */ protected $table = ''; /** * Constructor * @param DataReader / array $datareader The Data Reader object OR an array * @param int $text The number of rows in the Data Reader object * @param String $width The Width for the table * @param String $class The Class * @param String $border The Border * @param String $cellpadding The CellSpacing * @param String $cellspacing The CellPadding * @param String $summary The Summary * @param String $caption The Caption * @param String $id The element ID */ function __construct($datareader=null, $count='', $width='', $class='', $border='', $cellpadding='', $cellspacing='', $summary='', $caption='', $id='') { $theClass = $class != '' ? $class : CSS_COLOR_LIGHT; $theId = $id != '' ? $id : $this->getClassName()."Id"; parent::__construct($datareader, "", $width, $theClass, $border, $cellpadding, $cellspacing, $summary, $caption, $theId); $numRows = $count; if ($numRows !== '' && is_numeric($numRows)) { $this->numRows = $numRows; } else { $this->numRows = 0; $msg = 'Error, wrong object found: Expected numRows=numeric, found='.$numRows.", found=".(is_object($numRows) ? get_class($numRows) : $numRows); if (defined('HTML_BASIC_UTIL_PATH')) { Message::add($msg, __FILE__, __LINE__); } } $this->href = Server::getPhpSelf(); } public function setTable($table) { $this->table = $table; } /** * Get the Start page link * @return String The html */ function getStartPage() { $html = ''; $delta = Request::get(REQUEST_LIMIT_NAME_SKIP); if ($delta != 0) { $html .= $this->getLink(LIMIT_TEXT_START_PAGE, 0); } else { $html .= $this->getText(LIMIT_TEXT_START_PAGE); } return $html; } /** * Get the Previos page link * @return String The html */ function getPreviosPage() { $html = ''; $skip = 0; $delta = Request::get(REQUEST_LIMIT_NAME_SKIP); if (is_numeric($delta) && $delta > 0) { $skip = $delta - LIMIT_NUMBER_OF_ROWS; } else { } if ($delta != 0) { $html .= $this->getLink(LIMIT_TEXT_PREVIOS_PAGE, $skip); } else { $html .= $this->getText(LIMIT_TEXT_PREVIOS_PAGE); } return $html; } /** * Get the Page link * @param String $page The page number in question * @return String The html */ function getPage($page) { $html = ''; $skip = $page * LIMIT_NUMBER_OF_ROWS; $delta = Request::get(REQUEST_LIMIT_NAME_SKIP, 0); if ($skip != $delta) { $html .= $this->getLink($page + 1, $skip); } else { $html .= $this->getText($page + 1); } return $html; } /** * Get the Next page link * @return String The html */ function getNextPage() { $html = ''; $skip = LIMIT_NUMBER_OF_ROWS; $delta = Request::get(REQUEST_LIMIT_NAME_SKIP); if (is_numeric($delta) && $delta > 0) { $skip += $delta; } else { // Ignore } if ($skip < $this->numRows) { $html .= $this->getLink(LIMIT_TEXT_NEXT_PAGE, $skip); } else { $html .= $this->getText(LIMIT_TEXT_NEXT_PAGE); } return $html; } /** * Get the End page link * @return String The html */ function getEndPage() { $html = ''; $skip = LIMIT_NUMBER_OF_ROWS; $delta = Request::get(REQUEST_LIMIT_NAME_SKIP); if (is_numeric($delta) && $delta > 0) { $skip += $delta; } else { // Ignore } if ($skip < $this->numRows) { $modulus = $this->numRows % LIMIT_NUMBER_OF_ROWS; $max = $this->numRows-($modulus != 0 ? $modulus : LIMIT_NUMBER_OF_ROWS); $html .= $this->getLink(LIMIT_TEXT_END_PAGE, $max); } else { $html .= $this->getText(LIMIT_TEXT_END_PAGE); } return $html; } /** * Get the html for a link * @param String $text The text to show * @param String $skip The number of rows to skip * @param String $lines The number of lines to show * @param String $title The title for the link * @param String $layout The link layout to use, i.e. br, ul/li or line * @return String The html */ function getLink($text, $skip='', $lines='', $title='', $layout='') { $html = ''; $extra = array(); if ($skip != '') { $extra[REQUEST_LIMIT_NAME_SKIP] = $skip; } else { $extra[REQUEST_LIMIT_NAME_SKIP] = '0'; } if ($lines != '') { $extra[REQUEST_LIMIT_NAME_SHOW] = $lines; } if ($this->table != '') { $extra[@REQUEST_TABLE] = Encrypt::it( $this->table ); } $urlencode = true; $params = Params::get($extra, $urlencode, __FILE__, __LINE__); // $skip should be part as a request param, if not empty if ($layout === '') { $html .= " "; } $link = new Link($text, $this->href.$params, $this->class, $title, $layout); $html .= $link->getHtml(); return $html; } /** * Get the html for a plain text * @param String $text The text to show * @param String $layout The link layout to use, i.e. br, ul/li or line * @return String The html */ function getText($text, $layout='') { $html = ''; switch($layout) { case VIEW_LIMIT_LINK_LAYOUT_LI: $html .= '<li class="'.$this->class.'">'; break; } $html .= '<span class="'.$this->class.'"> '.$text." </span>\r\n"; switch($layout) { case VIEW_LIMIT_LINK_LAYOUT_LI: $html .= "</li>\r\n"; break; } return $html; } /** * Get the html for a the number of rows to show on each line * @return String The html */ function getNumberOfLinesToShow() { $html = ''; $lines = Request::get(REQUEST_LIMIT_NAME_SHOW, LIMIT_NUMBER_OF_ROWS); if (true) { // TODO add flag to show or not // TODO add config parameter if ul/li or br or line look and feel $html .= TEXT_NUMBER_ROWS_TO_SHOW; $layout = VIEW_LIMIT_LINK_LAYOUT; switch(VIEW_LIMIT_LINK_LAYOUT) { case VIEW_LIMIT_LINK_LAYOUT_LI: $html .= '<ul class="'.$this->class.'">'."\r\n"; break; } foreach($GLOBALS[DEFINE_LIMIT_NAME_SHOW] as $key=>$value) { if ($value > $this->numRows) { break; } if ($value == $lines) { $html .= $this->getText($value, $layout); } else { $title = ""; $html .= $this->getLink($value,'', $value, $title, $layout); } } switch(VIEW_LIMIT_LINK_LAYOUT) { case VIEW_LIMIT_LINK_LAYOUT_LI: $html .= "</ul>\r\n"; break; } //$html .= "<br />\r\n"; } return $html; } /** * Get the complete html for a navigation bar utilizing * mysql LIMIT x,y function * @return String The html */ function getHtml() { $html = $this->html; $rows = $this->numRows; if ($rows > 0 && ($this->numRows / LIMIT_NUMBER_OF_ROWS > 1)) { $html .= $this->getStart(); $tr = new Tr(); $html .= $tr->getStart(); $valign = 'top'; $colspan = '100 TODO'; // TODO $td = new Td($this->class, $valign, $colspan); $html .= $td->getStart(); $html .= $this->getStartPage(); $html .= $this->getPreviosPage(); for ($i = 0; $i < $this->numRows/LIMIT_NUMBER_OF_ROWS; $i++) { $html .= $this->getPage($i); } $html .= $this->getNextPage(); $html .= $this->getEndPage(); $html .= ' '; $html .= $td->getEnd(); $html .= $tr->getEnd(); $html .= $this->getEnd(); $html .= $this->getNumberOfLinesToShow(); } else { //$html .= "<br />\r\n"; } if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $html .= '<!-- '.$this->getClassName().'->getHtml(), (numRows:'.$this->numRows.') '; $html .= '(rows/max:'.($this->numRows/LIMIT_NUMBER_OF_ROWS).') '; $html .= '(max:'.LIMIT_NUMBER_OF_ROWS.') '; $html .= " -->\r\n"; } return $html; } /** * Display the html * <code> * Usage: * $text = count($numRows); * ViewLimit::display($datareader, $text, $width, $class, $border, $cellpadding, $cellspacing, $summary, $caption, $id); * </code> * @static * @param DataReader / array $datareader The Data Reader object OR an array * @param int $text The number of rows in the Data Reader object * @param String $width The width of the table * @param String $class The class of the table * @param String $border The border of the table * @param String $cellpadding The CellSpacing * @param String $cellspacing The CellPadding * @param String $summary The Summary * @param String $caption The Caption * @param String $id The element ID */ public static function display($datareader=null, $text='', $width='', $class='', $border='', $cellpadding='', $cellspacing='', $summary='', $caption='', $id='') { $html = new ViewLimit($datareader, $text, $width, $class, $border, $cellpadding, $cellspacing, $summary, $caption, $id); $html->addHtml(); }}?>
Den fulde HTML kildekode for ViewLimit klassen
<? <!-- DEBUG: ViewLimit --> <!-- ViewLimit->getHtml(), (numRows:0) (rows/max:0) (max:400) --> ?>
Her er 'klasse metoderne' for ViewLimit klassen:
Her er 'objekt variable' for ViewLimit klassen: