WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Dynamic select menu
Categories : PHP, Arrays, HTML and PHP Click here to Update Your Picture
Olaf Lederer
Date : Feb 11th 2005
Grade : 2 of 5 (graded 10 times)
Viewed : 6567
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Olaf Lederer
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

In a lot of dynamic forms or content management sytems the form element "SELECT" is used frequently. If you use your PHP code inside the standard HTML element the code will be nearly unreadable. Adding extra options is not really fun. This small handy function will produce a select based on an associative array. Just define an array and call the function, that's all.

<?php

// build here the array with values for the select,
// notice that the array key is used option value and the array value as the label.
$test_array = array("var_1"=>"first label", "some_var"=>"it's hot", "last_constant"=>"last element");

// the properties of this function the array above, the name of the select menu and the initial value.
// If the initial value is empty (by default the an empty option is added otherwise.
// Use only a valid key from the above array as initial value and the selected state is used for this value.
function dynamic_select($the_array, $element_name, $init_value = "") {
   
$menu = "<select name=\"".$element_name."\">\n";
    if (!isset(
$_REQUEST[$element_name])) {
        if (
$init_value == "") {
           
$menu .= "  <option value=\"\">...\n";
        } else {
           
$curr_val = $init_value;
        }
    } else {
       
$curr_val = $_REQUEST[$element_name];
    }
    foreach (
$the_array as $key => $value) {
       
$menu .= "  <option value=\"".$key."\"";
       
$menu .= ($key == $curr_val) ? " selected" : "";
       
$menu .= ">".$value."\n";
    }
   
$menu .= "</select>\n";
    return
$menu;
}
/* Example:
echo create_select($test_array, "test_menu", "some_var");

will output this:
<select name="test_menu">
  <option value="var_1">first label
  <option value="some_var" selected>it's hot
  <option value="last_constant">last element
</select>
*/
?>



Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
How to pass an array from one PHP Script to another via an HTML form
Categories : PHP, HTML and PHP, Arrays
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
Print out array key => value in colored HTML
Categories : PHP, Arrays, HTML and PHP
Form Submission Using Array's
Categories : PHP, HTML and PHP, Beginner Guides, Arrays
Parsing html tags with php. Get an array from this function
Categories : PHP, HTML and PHP, Arrays, Tag Extractors
CSS style switcher
Categories : PHP, CSS, HTML and PHP, Arrays, Sessions
navbar.php3 - Dynamic hyperlinked navigation bars
Categories : HTML and PHP, Arrays, PHP, Complete Programs
PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use!
Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP
Simple script to passing persistent and growing array between recalls of one page (manipulate little stack).
Categories : Arrays, Global Variables, PHP, HTML and PHP, Variables
Select with current month
Categories : PHP, HTML and PHP, Date Time, Arrays
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
a function that builds an HTML select list from any mysql table.
Categories : PHP, MySQL, HTML and PHP
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET
Categories : Algorithms, HTML and PHP, PHP, Variables