[PHP-CVS] cvs: php4 /pear/HTML Select.php
adaniel Fri Mar 23 20:34:18 2001 EDT Modified files: /php4/pear/HTML Select.php Log: added comments to the HTML output, added loadQuery, added load Index: php4/pear/HTML/Select.php diff -u php4/pear/HTML/Select.php:1.2 php4/pear/HTML/Select.php:1.3 --- php4/pear/HTML/Select.php:1.2 Thu Mar 22 22:24:07 2001 +++ php4/pear/HTML/Select.php Fri Mar 23 20:34:17 2001 @@ -16,7 +16,7 @@ // | Authors: Adam Daniel [EMAIL PROTECTED]| // +--+ // -// $Id: Select.php,v 1.2 2001/03/23 06:24:07 adaniel Exp $ +// $Id: Select.php,v 1.3 2001/03/24 04:34:17 adaniel Exp $ require_once "DB.php"; require_once "PEAR.php"; @@ -26,7 +26,7 @@ * Class to dynamically create an HTML SELECT * * @author Adam Daniel [EMAIL PROTECTED] - * @version 1.0 + * @version 1.1 * @sincePHP4.04pl1 * @access public */ @@ -54,13 +54,13 @@ /** * Class constructor * - * @param string$name Name attribute of the SELECT - * @param int $size Size attribute of the SELECT - * @param bool $multiple Whether the select will allow multiple + * @param string$name (optional)Name attribute of the SELECT + * @param int $size (optional) Size attribute of the SELECT + * @param bool $multiple (optional)Whether the select will allow +multiple * selections or not - * @param mixed $attributes Either a typical HTML attribute string + * @param mixed $attributes (optional)Either a typical HTML attribute +string * or an associative array - * @param int $tabOffset Number of tabs to offset HTML source + * @param int $tabOffset (optional)Number of tabs to offset HTML source * @since 1.0 * @accesspublic * @returnvoid @@ -86,7 +86,7 @@ */ function apiVersion() { -return 1.0; +return 1.1; } //end func apiVersion /** @@ -140,9 +140,6 @@ } $attributes = $this-_parseAttributes($attributes); $attr = array("value"=$value); -if (in_array($value, $this-_values)) { -$attr[] = "SELECTED"; -} $this-_updateAttrArray($attributes, $attr); $this-_options[] = array("text"=$text, "attr"=$attributes); } // end func addOption @@ -166,11 +163,7 @@ $this-setSelectedValues($values); } while (list($key, $value) = each($arr)) { -if (in_array($value, $this-_values)) { -$this-addOption($key, $value, true); -} else { -$this-addOption($key, $value); -} +$this-addOption($key, $value); } return true; } // end func loadArray @@ -188,7 +181,7 @@ * @returnPEAR_Error on error or true * @throwsPEAR_Error */ -function loadDbResult($result, $textCol="", $valueCol="", $values=null) +function loadDbResult($result, $textCol=null, $valueCol=null, $values=null) { if (!is_object($result) || (get_class($result) != "db_result" is_subclass_of($result, "db_result"))) { @@ -200,22 +193,77 @@ $fetchMode = ($textCol $valueCol) ? DB_FETCHMODE_ASSOC : DB_FETCHMODE_DEFAULT; while (is_array($row = $result-fetchRow($fetchMode)) ) { if ($fetchMode == DB_FETCHMODE_ASSOC) { -if (in_array($row[$valueCol], $this-_values)) { -$this-addOption($row[$textCol], $row[$valueCol], true); -} else { -$this-addOption($row[$textCol], $row[$valueCol]); -} +$this-addOption($row[$textCol], $row[$valueCol]); } else { -if (in_array($row[0], $this-_values)) { -$this-addOption($row[0], $row[1], true); -} else { -$this-addOption($row[0], $row[1]); -} +$this-addOption($row[0], $row[1]); } } return true; } // end func loadDbResult + +/** + * Queries a database and loads the options from the results + * + * @param mixed $conn Either an existing DB connection or a valid +dsn + * @param string$sqlSQL query string + * @param string$textCol(optional) Name of column to display as the +OPTION text + * @param string$valueCol (optional) Name of column to use as the +OPTION value + * @param mixed $values (optional) Array or comma delimited string of +selected values + * @since 1.1 + * @accessprivate + * @returnvoid + * @throws + */ +function loadQuery($conn,
[PHP-CVS] cvs: php4 /pear/HTML Select.php
adaniel Thu Mar 22 22:18:12 2001 EDT Added files: /php4/pear/HTML Select.php Log: original commit. Basic html select loaded manually, from a DB result, or an array Index: php4/pear/HTML/Select.php +++ php4/pear/HTML/Select.php ?php require_once "DB.php"; require_once "PEAR.php"; require_once "HTML/Common.php"; /** * Class to dynamically create an HTML SELECT * * @author Adam Daniel [EMAIL PROTECTED] * @version 1.0 * @sincePHP4.04pl1 * @access public */ class HTML_Select extends HTML_Common { /** * Contains the select options * * @var array * @since 1.0 * @accessprivate */ var $_options = array(); /** * Default values of the SELECT * * @var string * @since 1.0 * @accessprivate */ var $_values = array(); /** * Class constructor * * @param string$name Name attribute of the SELECT * @param int $size Size attribute of the SELECT * @param bool $multiple Whether the select will allow multiple * selections or not * @param mixed $attributes Either a typical HTML attribute string * or an associative array * @param int $tabOffset Number of tabs to offset HTML source * @since 1.0 * @accesspublic * @returnvoid * @throws */ function HTML_Select($name="", $size=1, $multiple=false, $attributes=null, $tabOffset=0) { HTML_Common::HTML_Common($attributes, $tabOffset); $attr = array("name"=$name, "size"=$size); if ($multiple) { $attr[] = "MULTIPLE"; } $this-updateAttributes($attr); } // end constructor /** * Returns the current API version * * @since 1.0 * @accesspublic * @returndouble * @throws */ function apiVersion() { return 1.0; } //end func apiVersion /** * Sets the default values of the select box * * @param mixed$values Array or comma delimited string of selected values * @since 1.0 * @accesspublic * @returnvoid * @throws */ function setSelectedValues($values) { if (is_string($values)) { $values = split("[ ]?,[ ]?", $values); } $this-_values = $values; } //end func setSelectedValues /** * Returns an array of the selected values * * @since 1.0 * @accesspublic * @returnarray of selected values * @throws */ function getSelectedValues() { return $this-_values; } // end func getSelectedValues /** * Adds a new OPTION to the SELECT * * @param string$text Display text for the OPTION * @param string$value Value for the OPTION * @param bool $selected Whether the option is selected or not * @param mixed $attributes Either a typical HTML attribute string * or an associative array * @since 1.0 * @accesspublic * @returnvoid * @throws */ function addOption($text, $value, $selected=false, $attributes=null) { if ($selected !in_array($value, $this-_values)) { $this-_values[] = $value; array_unique($this-_values); } $attributes = $this-_parseAttributes($attributes); $attr = array("value"=$value); if (in_array($value, $this-_values)) { $attr[] = "SELECTED"; } $this-_updateAttrArray($attributes, $attr); $this-_options[] = array("text"=$text, "attr"=$attributes); } // end func addOption /** * Loads the options from an associative array * * @param array$arr Associative array of options * @param mixed$values (optional) Array or comma delimited string of selected values * @since 1.0 * @accesspublic * @returnPEAR_Error on error or true * @throwsPEAR_Error */ function loadArray($arr, $values=null) { if (!is_array($arr)) { return new PEAR_ERROR("First argument to HTML_Select::loadArray is not a valid array"); } if (isset($values)) { $this-setSelectedValues($values); } while (list($key, $value) = each($arr)) { if (in_array($value, $this-_values)) { $this-addOption($key, $value, true); } else { $this-addOption($key, $value); } } return true; } // end func loadArray /** * Loads the options from DB_result object * * If no column names are specified the first two
[PHP-CVS] cvs: php4 /pear/HTML Select.php
adaniel Thu Mar 22 22:24:07 2001 EDT Modified files: /php4/pear/HTML Select.php Log: forgot the license header Index: php4/pear/HTML/Select.php diff -u php4/pear/HTML/Select.php:1.1 php4/pear/HTML/Select.php:1.2 --- php4/pear/HTML/Select.php:1.1 Thu Mar 22 22:18:12 2001 +++ php4/pear/HTML/Select.php Thu Mar 22 22:24:07 2001 @@ -1,7 +1,27 @@ ?php +/* vim: set expandtab tabstop=4 shiftwidth=4: */ +// +--+ +// | PHP version 4.0 | +// +--+ +// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group | +// +--+ +// | This source file is subject to version 2.0 of the PHP license, | +// | that is bundled with this package in the file LICENSE, and is| +// | available at through the world-wide-web at | +// | http://www.php.net/license/2_02.txt. | +// | If you did not receive a copy of the PHP license and are unable to | +// | obtain it through the world-wide-web, please send a note to | +// | [EMAIL PROTECTED] so we can mail you a copy immediately. | +// +--+ +// | Authors: Adam Daniel [EMAIL PROTECTED]| +// +--+ +// +// $Id: Select.php,v 1.2 2001/03/23 06:24:07 adaniel Exp $ + require_once "DB.php"; require_once "PEAR.php"; require_once "HTML/Common.php"; + /** * Class to dynamically create an HTML SELECT * -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]