Login   Register  
PHP Classes
elePHPant
Icontem

File: formselect.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Roberto Bertˇ  >  FormSelect - <SELECT> tag generation  >  formselect.class.php  >  Download  
File: formselect.class.php
Role: ???
Content type: text/plain
Description: The Class, Documentation, Examples, Version Control and the class
Class: FormSelect - <SELECT> tag generation
Create <SELECT> tags with associative arrays.
Author: By
Last change:
Date: 12 years ago
Size: 5,738 bytes
 

Contents

Class file image Download
<?PHP

/*
FormSelect version 1.2 - 3/MAI/2002
Last version: http://phpclasses.upperdesign.com/browse.html/author/8382

Created By Roberto Bertˇ (darkelder@users.sourceforge.net) under LGPL

----------------------------------------------------------------------------------
OVERVIEW ---
The FormSelect is a class to create <SELECT> tags with associative arrays or index arrays. The class
can sort the array, and you can customize the tag.

READ FIRST
----------------------------------------------------------------------------------

-- An Associative array have the format:
array(
	"VALUE"		 => "KEY",
	"VALUE2" 	 => "KEY2"
);

WHERE

VALUE is the <option value=key>VALUE</option>, or, in other words, what the user will SEE in the browser
KEY is the <option value=KEY>value</option> what Select will return to PHP


-- The No-Associtive have the format:
array("VALUE","VALUE2")

WHERE

VALUE will be <option value=VALUE>VALUE</option> - the BOTH things


----------------------------------------------------------------------------------
CHANGELOG ---
12/SET/2001 - 1.0 - First Realease
13/SET/2001 - 1.1 - Use of htmlentities() to fix some bugs
03/MAI/2001 - 1.2 - Removed a bug that print wrong name attribute of select. (by Chris Paul). License
                    changed to LGPL


----------------------------------------------------------------------------------
DOCUMENTATION ---
	var $name
		value of the tag name=""

	var $values
		the array of select

	var $defaul
		the default value of select that should be selected - leave blank if you not want pre select any option

	var $selected
		0 - the default value is on values part of "$this->values[values] = $keys"
		1 - the default value is on keys part of "$this->values[values] = $keys"

	var $type
		0 if the array $this->values isn't an associative array
		1 if the array $this->values is an associative array

	var $sort
		0 = dont sort
		1 = sort the array

	var $more
		add something "<select ... HERE>"
		You can use this var to set some javascript code, like, onchage="this.form.submit()"; or to style="color: #FF00FF" or to class="selclass" or both things.

	var $size
		value of the tag size=""

	function create()
		create the SELECT tag

		syntX:
		string create()

----------------------------------------------------------------------------------
EXAMPLES ---
	Examples:
	- An no associative array
		<?PHP
		//include_once("formselect.php");
		$FormSelect				= new FormSelect;
		$FormSelect->name		= "test";
		$FormSelect->values		= array("Select:","Blue","Orange","Red","Pink");
		$FormSelect->default	= "Select:";
		$FormSelect->type		= 0;
		$FormSelect->sort		= 1;
		// alert the value of Selected Index
		$FormSelect->more		= "onchange=\"alert(this.options[this.selectedIndex].value)\"";

		print printf("<FORM>%s</FORM>",$FormSelect->create());
		?>

	- An associative array do the same but better:
		<?PHP
		//include_once("formselect.php");
		$FormSelect				= new FormSelect;
		$FormSelect->name		= "test";
		$FormSelect->values		= array("Select" => "", "Download" => "http://www.download.com", "Altavista" => "http://www.altavista.com", "QUE.com.br" => "http://www.que.com.br");
		$FormSelect->type		= 1;
		$FormSelect->sort		= 1;
		// alert the value of Selected Index
		$FormSelect->more		= "onchange=\"if (this.options[this.selectedIndex].value) top.location = this.options[this.selectedIndex].value\"";


		print printf("<FORM>%s</FORM>",$FormSelect->create());
		?>

----------------------------------------------------------------------------------
THE CLASS ---
*/

if (!defined('FORMSELECT_DEFINED') ) {
	define('FORMSELECT_DEFINED', TRUE);

	class FormSelect {

		// value of the tag name=""
		var $name		= "";


		// the array of select
		var $values		= array();


		// the default value of select that should be selected - leave blank if you not want pre select any option
		var $default	= "";


		// 0 - the default value is on values part of "$this->values[keys] = $values"
		// 1 - the default value is on keys part of "$this->values[keys] = $values"
		var $selected	= 1;


		// 0 if the array $this->values isn't an associative array
		// 1 if the array $this->values is an associative array
		var $type		= 0;


		// 0 = dont sort
		// 1 = sort the array
		var $sort		= 0;


		// add something "<select ... HERE>"
		var $more		= "";


		// value of the tag size=""
		var $size		= 1;


		// return the created <select> tag
		function create () {
			// if name have some " > or other html special tag, transform to html encodeded chars.
			$this->name = htmlentities($this->name);

			// create the main tag
			$t = "\n\n<select name=\"{$this->name}\" size=\"$this->size\" $this->more>\n";

			// sort the array
			if ($this->sort == 1) {
				if ($this->type == 1) asort($this->values);
				else sort($this->values);
			}

			// reset the array to print all values
			reset($this->values);

			// if values have some data inside
			if (sizeof($this->values) > 0) {
				while (list($values_value,$values_key) = each($this->values)) {
					unset($c);
					// if is an associative array
					if ($this->type == 0) {
						$values_value = $values_key;
					}

					// if default, print selected
					if ($values_value == $this->default & $this->selected == 0) { $c = " selected"; }
					elseif ($values_key == $this->default & $this->selected == 1) { $c = " selected"; }
					$t .= "<option value=\"" . htmlentities($values_key) . "\"$c>" . htmlentities($values_value) . "</option>\n";
				}
			}
			$t .= "</select>\n\n";
			return $t;
		}
	}
}

?>