T4VN is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support
    HOME  |  HOSTING COUPON  |  TEMPLATE  |  PHP SCRIPTS  |  LINK TO US  |  LINK  |  REGISTER | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Tutotal :::
  PHP Basic (7)
  PHP Advanced (4)
  PHP Database (2)
  Coding Step By Step (8)
  PHP and AJAX (0)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (12)
  Date & Time (5)
  E-Mail (6)
  Forms (5)
  Guestbooks (1)
  Logging (2)
  Miscellaneous (10)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (10)
  User Authentication (6)
::: Search On T4VN :::
::: T4VN Statistics :::
  PHP Scripts : 61
  PHP Example : 67
  PHP Tutorials : 21
  PHP News : 93
  Total Coupon : 36
  Other Tutorials :
  Member : 209
  Visitor Online : 5
  Today Visit: 309
  Total Visitor : 262319
  Most Online : 31
::: Sponsored Links :::

PR 4 For This Webpage
Home Today :

Build Select Boxes from arrays

Author : Dave_Morton

The buildSelect()function takes two arguments (one for the list, and one for options) and builds a selectbox for your form. The first array is simply the list you want to display. There are several examples within the script, so syntax shouldn't be a hassle. The second array is simply an options list. The first two elements are for adding an optional 'Title' to the beginning of the list, and whether is's selected by default. The third, and any subsequent elements of the options array, for that matter, choose which elements of the list are selected ( more than one can be applied to multiple selectboxes) on display

PHP Example :

<html>
<head>
  <title>PHP select box constructor demo</title>
</head>
<? // build arrays for select boxes
$one = array(
             
"1"       =>  "Choice one",
             
"2"       =>  "Choice two",
             
"3"       =>  "Choice three"
            
);

$two = array(
             
"one"   =>  "#1",
             
"two"   =>  "#2",
             
"three" =>  "#3"
            
);

$three = array(
               
"T"     =>     "Top",
               
"M"     =>     "Middle",
               
"B"     =>     "Bottom"
              
);

$four = array (
               
"grla"     => "Gorilla",
               
"ostr"     => "Ostritch",
               
"pthn"     => "Python",
               
"shrk"     => "Shark",
               
"spdr"     => "Spider"
              
);

function 
buildSelect($selArr$selOpt){
    
$selectText $selOpt[0];
    if (
$selectText != "") {
        
$selDefault "selected";
        if (
$selOpt[1] == "true") {
            
$selDefault "";
        }
        echo 
"<option $selDefault>$selectText</option>n";
    }
    foreach(
$selArr as $key => $value){
    
$sel "";
        for (
$loop 2$loop count($selOpt); $loop++) {
            if (
$selOpt[$loop] == $key) {
                
$sel "selected";
            }
        }
        echo 
"<option $sel value='$key'>$value</option>n";
    }
    return;
}
?>
<body>
<form method="POST" action="">
<table bordercolor="#000000" cellspacing="1" cellpadding="2" border="1">
  <tr>
    <td class="left">Select Box #1:</td>
    <td class="right">
        <select name="one">
<? buildSelect($one, array("Select an option""true""")) ?>
        </select>
    </td>
  </tr>
  <tr>
    <td class="left">Select Box #2:</td>
    <td class="right">
        <select name="two">
<? buildSelect($two, array("Pick one""false""")) ?>
        </select>
    </td>
  </tr>
  <tr>
    <td class="left">Select Box #3:</td>
    <td class="right">
        <select name="three">
<? buildSelect($three, array("""""M")) ?>
        </select>
    </td>
  </tr>
  <tr>
    <td class="left">Select Box #4:</td>
    <td class="right">
        <select name="four" multiple size="<?echo count($four) - 2 ?>">
<?
    $options 
= array( """""grla""pthn");
    
buildSelect($four$options)
?>
        </select>
    </td>
  </tr>
</table>
</form>
</body>
</html>



Thank for Dave_Morton

Other Example


::: Resources :::
  Links Directory
  Programming 2 3
  Webmaster 2 3
  Web Design 2 3
  Web Hosting 2 3
  Other Links 2 3
  Asian ShowBiz News
  Teach Seo For You
::: New Templates :::




::: Other Tutorials :::
 Program Design

  Powered By T4VN.NET - Version 2.0 - CopyRight © T4VN.NET 2005-2007