Put your tips and tricks online - Share your knowledge! Login | Register
 
 
  Search     Advanced search
 

Home | Ask Question | Add tip | My tips | Recent tips & tricks | Suggest a category | FAQ | Forums

 
 
 
 Category : Home > Programming > PHP > HTML     

PHP HTML class / printing SELECT dropdown box


/*
Here's a very handy class function for printing HTML SELECT. I normally use it with dynamic
arrays i.e. result set from an SQL query
*/

class html {

	function print_select($select_name, $select_array, $selected_value="", $js="", $multiple="")
{
		//
		// print a select box
		// parameters: 
		// 1. name of the select box
		// 2. array with keys and values
		// 3. selected value(s)
		// 4. javascript
		// 5. multiple
		//

		print ( "\n<SELECT NAME=\"" . $select_name . "\" " .
						$multiple . " " . $js . ">\n" );

		while (list ($key, $val) = each ($select_array)) {
			if ($multiple == "MULTIPLE") {
				if (is_array($selected_value)) {
					if (in_array($key, $selected_value)) {
						print ( "\t<OPTION VALUE=\"" .
						$key . "\" SELECTED>" . $val ."</OPTION>\n" );
					} else {
						print ( "\t<OPTION VALUE=\"" .
						$key . "\">" . $val ."</OPTION>\n" );
					}
				} else {
					print ( "\t<OPTION VALUE=\"" .
					$key . "\">" . $val ."</OPTION>\n" );
				}
			} else {
				if ($key == $selected_value) {
					print ( "\t<OPTION VALUE=\"" .
					$key . "\" SELECTED>" . $val ."</OPTION>\n" );
				} else {
					print ( "\t<OPTION VALUE=\"" .
					$key . "\">" . $val ."</OPTION>\n" );
				}
			}
		}

		print ( "</SELECT>\n" );

	}

	//
	// Put some other functions here ...
	//

}


/*

Usage example:

*/

// This is the file with the class
include_once("html.inc.php");

// Initiate the class
$html = new html;

// Get the result set from SQL query
// Some database related code here ... put the results into $result_array

// Print the select box
$html->print_select("select_name", $result_array, $selected_element,
"OnChange=\"this.form.submit();\"");


  Options
 
   del.icio.us  |  newsvine  |  digg  |  furl  |  google  |  yahoo  |  Ma.gnolia  |  vigillar  |  reddit  |  technorati  |  icerocket  |  pubsub

     (Average: 4.89 / Votes: 18)   Rate this tip:    

  Support Forum  Posted by 
   Another function for HTML class       agoikhman  

Start new discussion or add comment to this tip

  Details
Tip reference : #30
views : 1066
Added on : 10/26/05
Submited by : h8dk97
 
Send a message Send a message Printer friendly output Printer friendly output
Display this member's tips Display this member's tips (163)
 
 
 Most viewed tips 
  Databases > Oracle > Security : How to unlock Oracle user account  
  Operating Systems > Unix : How to kill Unix user session  
  Databases > Oracle > Performance Tuning : How to enable trace in Oracle  
  Databases > Oracle : Kill user session  
   
  All categories
Databases | Programming | Hardware | Operating Systems | Networking | Internet | ERP / CRM | Games & Multimedia | Graphics & Design | Miscellaneous | Office Software | TipLib FAQ
 
 

Home |  FAQ |  Terms of Use |  Privacy Policy

© 2005 tiplib.com