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
* @since PHP4.04pl1
* @access public
*/
class HTML_Select extends HTML_Common
{
/**
* Contains the select options
*
* @var array
* @since 1.0
* @access private
*/
var $_options = array();
/**
* Default values of the SELECT
*
* @var string
* @since 1.0
* @access private
*/
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
* @access public
* @return void
* @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
* @access public
* @return double
* @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
* @access public
* @return void
* @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
* @access public
* @return array 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
* @access public
* @return void
* @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
* @access public
* @return PEAR_Error on error or true
* @throws PEAR_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 columns of the result are
* used as the text and value columns respectively
* @param object $result DB_result object
* @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.0
* @access public
* @return PEAR_Error on error or true
* @throws PEAR_Error
*/
function loadDbResult(&$result, $textCol="", $valueCol="", $values=null)
{
if (!is_object($result) || (get_class($result) != "db_result" &&
is_subclass_of($result, "db_result"))) {
return new PEAR_ERROR("First argument to HTML_Select::loadDbResult is not
a valid DB_result");
}
if (isset($values)) {
$this->setSelectedValues($values);
}
$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]);
}
} else {
if (in_array($row[0], $this->_values)) {
$this->addOption($row[0], $row[1], true);
} else {
$this->addOption($row[0], $row[1]);
}
}
}
return true;
} // end func loadDbResult
/**
* Returns the SELECT in HTML
*
* @since 1.0
* @access public
* @return string
* @throws
*/
function toHtml()
{
$tabs = $this->_getTabs();
$strHtml =
"\n" .$tabs . "<SELECT" . $this->_getAttrString($this->_attributes) .
">\n";
for ($counter=0; $counter < count($this->_options); $counter++) {
$strHtml .=
$tabs . "\t<OPTION" .
$this->_getAttrString($this->_options[$counter]["attr"]) . ">" .
$this->_options[$counter]["text"] . "\n";
}
$strHtml .= $tabs . "</SELECT>";
return $strHtml;
} // end func toHtml
} // end class 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]