baaSelect User Guide


  baaSelect Create new baaSelect object
  addSelect add <SELECT> component to the object
  makeScript Generate the javascript data arrays and function code
  makeSelect Generate html code for the <SELECT> component
Code samples and tutorial  
Download class  
Download guide  

Method Reference

baaSelect object baaSelect ([dbtype[,dbConnection]])
  dbtype [optional] the type of database you are using. Default is MySQL
  dbConnection [required for ODBC only] the link resource returned by odbc_connect()

Pre-defined constants for dbtype are

MySql database (default)
PostgreSQL database
Any database via odbc connection
MS SQL database
Sybase database

addSelect(selectName, srcTable, idField, descripField [, fkField [, order [, defaultText]]] )
  selectName Name to be give to your dropdown

as in <SELECT name="selectName">

  srcTable Table containing the data used to populate the dropdown

  idField Field in srcTable to be used as the option values

  descripField Field in srcTable to be used for the dropdown display text

  fkField [optional] Foreign key field in srcTable which links the option to the parent dropdown's selected value

Add your selects in hierachical sequence

Always empty in the first dropdown added. If it is left blank in other drowdowns it will indicate that it is not a related dropdown and the values will be fetched from the srcTable and it will not be updated when parent value changes

  order [optional] Sort order for option items

Can be 'BY_ID' or 'BY_TEXT'. Default is 'BY_TEXT'

  defaultText [optional] Text display when no values are selected. Default is blank


  No parameters Place this method call in the 'head' section of your page to generate the required javascript code

	<? $sel->makeScript() ?>

  selectName Name of the select dropdown to be generated

Generates the HTML code for you dropdown. Must be within <form> tags

	<form method='post' action='some.php'>

Sample Code

CREATE TABLE `region` (
    `regionID` int(11) NOT NULL auto_increment,
    `region` varchar(20) default NULL,
    PRIMARY KEY  (`regionID`)
    ) TYPE=MyISAM;
INSERT INTO region (region) 
	('East Anglia'), 
   leaID int not null auto_increment primary key,
   lea varchar(20),
   regionID int) ;
INSERT INTO lea (regionID, lea) 
   (2, 'Barking and Dagenham'), 
   (2, 'Barnet') ;
Sample PHP code
$cnx = mysql_connect('localhost');

include 'baaSelect.php';
$sel = new baaSelect();

$sel->addSelect('region', 'region', 'regionID', 'region','',1,'--region--');
$sel->addSelect('lea', 'lea', 'leaID', 'lea','regionID',1,'--select--');

<meta Name="generator" content="PHPEd Version 3.1.2 (Build 3165)">

<? $sel->makeScript() ?>

<form method=get>
$sel->makeSelect('region');         # this creates the HTML
Generated HTML code
<meta Name="generator" content="PHPEd Version 3.1.2 (Build 3165)">

<SCRIPT language="JavaScript" type="text/javascript">

var currentregion = 0 ;
var currentlea = 0 ;

var arrayleaTXT = new Array();
var arrayleaVAL = new Array();
arrayleaTXT[0] = "";
arrayleaVAL[0] = 0;
arrayleaTXT[1] = "Cambridgeshire|Norfolk|Peterborough|Suffolk";
arrayleaTXT[2] = "Barking and Dagenham|Barnet";

arrayleaVAL[1] = "4|1|2|3";
arrayleaVAL[2] = "5|6";

function baaSelectUpdatelea (form) {

               currentregion = form.region.options[form.region.selectedIndex].value;

               while (form.lea.options.length) {
                    form.lea.options[0] = null;
               var tmp = new String (arrayleaTXT[currentregion]);
               var arrayText = tmp.split("|");
               tmp = new String (arrayleaVAL[currentregion]);
               var arrayVals = tmp.split("|");

               var optionlist = form.lea.options;
               optionlist[0] = new Option();
               optionlist[0].value = 0;
               optionlist[0].text = "--select--";
               if (arrayleaTXT[currentregion]) {
                   for (var i=0; i<arrayText.length; i++) {
                        optionlist[i+1]=new Option();
                        optionlist[i+1].value = arrayVals[i];
                        optionlist[i+1].text = arrayText[i];
                        if (currentlea == arrayVals[i])
                            optionlist[i+1].selected = true;


// generated by baaSelect.php  -->

<form method=get>
<SELECT name="region" onchange="baaSelectUpdatelea(this.form)" >
<OPTION value="0" > --region--</option>
<OPTION value="1" > East Anglia</option>
<OPTION value="2" > London</option>
<SELECT name="lea" >
<OPTION value="0" > --select--</option>

 Barry Andrew 2004