Forums

PHP

This topic is locked

Dynamic DropDown lists

Posted 27 Jun 2003 02:53:27
1
has voted
27 Jun 2003 02:53:27 waleed ware posted:
Hi
I`ve found a good script for the above mentioned problem.

Still I have a problem that i can`t populate the primary and secondary dropdowns with an array taken from a mysql database tables:

here is where the array should be put
<pre id=code><font face=courier size=2 id=code>
$Akey_ID = $areg['aregID'];
$Acat = $areg['atype'];
$Asubcat = $areg['areg'];
$num_in_array = $totalRows_rsReg;
$Adistinct_cats = $areg['atype'];
$max_items = $totalRows_rsReg;
</font id=code></pre id=code>

and here is the code generated by DWMX

Please anyone can help out.
Thanks
<pre id=code><font face=courier size=2 id=code>
&lt;?php require_once('Connections/connLogbook.php'); ?&gt;
&lt;?php
mysql_select_db($database_connLogbook, $connLogbook);
$query_rsReg = "SELECT * FROM areg, atype WHERE areg.atypeID = atype.atypeID ORDER BY areg.areg ASC";
$rsReg = mysql_query($query_rsReg, $connLogbook) or die(mysql_error());
$row_rsReg = mysql_fetch_assoc($rsReg);
$totalRows_rsReg = mysql_num_rows($rsReg);
$areg = mysql_fetch_array($rsReg);


### this neat javascript function from Leon Atkinson updates the second dropdown box
### (subcategory) depending on what is selected in the first (category).
### this function obviously needs to be contained in a form.
###
### array_key_ID is an array of the unique identifiers of the subcategory
### array_cat is the array of categories
### array_subcat is the array of the subcategories
### num_in_array is the total number of items in the array
### distinct_cats is the number of unique items in the category field
### MaxItems is the maximum number of items that can occur in the subcategory dropdown box
###
### Works in IE 4+ and Mozilla 0.9.9+
### Not currently working in Opera (6.02)

function dynamicboxes ($array_key_ID, $array_cat, $array_subcat, $num_in_array, $distinct_cats, $MaxItems ){

print "&lt;SCRIPT Language=\"JavaScript\"&gt;\n";
print "function UpdateSubCat()\n";
print "{\n";
print "\tvar SubCat = document.choose_category.subcat.options.length;\n";
print "\twhile(SubCat &gt; 0)\n";
print "\t{\n";
print "\t\tSubCat--;\n";
print "\t\tdocument.choose_category.subcat.options[SubCat].text = '';\n";
print "\t\tdocument.choose_category.subcat.options[SubCat].value = '';\n";
print "\t}\n\n";
print "\tdocument.choose_category.subcat.options[0].selected = true;\n\n";
print "\tvar CatSelected = 0;\n";
print "\twhile(document.choose_category.cat.options[CatSelected].selected==false) CatSelected++;\n\n";

$StoreCat = "";
$SubCatCount = 0;
$RowCount = 0;

while($RowCount &lt; $num_in_array)
{
$item_key_ID = $array_key_ID[$RowCount];
$item_Cat = $array_cat[$RowCount];
$item_SubCat = $array_subcat[$RowCount];

if($StoreCat != $item_Cat)
{
if($StoreCat != ""
{
print "\t}\n";
}

print "\tif(document.choose_category.cat.options[CatSelected].value == '$item_Cat')\n";
print "\t{\n";
print "\t\tdocument.choose_category.subcat.options[0].value = 0;\n";
print "\t\tdocument.choose_category.subcat.options[0].text = 'Choose a subcategory';\n\n";

$StoreCat = $item_Cat;
$SubCatCount=1;
}

print "\t\tdocument.choose_category.subcat.options[$SubCatCount].value = '$item_key_ID';\n";
print "\t\tdocument.choose_category.subcat.options[$SubCatCount].text = '$item_SubCat';\n\n";
$SubCatCount++;
$RowCount++;
}

print "\t}\n";
print "}\n";
print "&lt;/SCRIPT&gt;\n\n";
print "&lt;SELECT NAME=cat onChange=\"UpdateSubCat();\"&gt;\n";
print "&lt;OPTION VALUE=0&gt;Choose category";
$RowCount = 0;
while($RowCount &lt; count($distinct_cats))
{
print "&lt;OPTION VALUE=\"";
print $distinct_cats[$RowCount];
print "\"&gt;";
print $distinct_cats[$RowCount];
print "\n";
$RowCount++;
}

print "&lt;/SELECT&gt;\n";

print "&lt;SELECT NAME=subcat &gt;\n";
$RowCount = 0;
while($RowCount &lt;= $MaxItems)
{
print "&lt;OPTION VALUE=$RowCount&gt;";
print "Subcategory\n";
$RowCount++;
}
print "&lt;/SELECT&gt;";
print "&lt;SCRIPT Language=\"JavaScript\"&gt;UpdateSubCat();&lt;/SCRIPT&gt;\n";

}
$Akey_ID = $areg['aregID'];
$Acat = $areg['atype'];
$Asubcat = $areg['areg'];
$num_in_array = $totalRows_rsReg;
$Adistinct_cats = $areg['atype'];
$max_items = $totalRows_rsReg;

print"&lt;HTML&gt;";

#### Collect the submitted values, then print them:
if (count($HTTP_POST_VARS)) {
while (list($key, $value) = each ($HTTP_POST_VARS)) {
$arr_request[$key] = $value;
}
}
if ($arr_request[cat]){print $arr_request[cat]." - ".$arr_request[subcat];}


#### Main Form

print "&lt;FORM NAME=choose_category ACTION=$PHP_SELF METHOD=POST&gt;";

dynamicboxes($Akey_ID, $Acat, $Asubcat, $num_in_array, $Adistinct_cats, $max_items );

print "&lt;BR&gt;&lt;BR&gt;&lt;INPUT TYPE=\"submit\"&gt;&lt;BR&gt;\n";
print "&lt;/FORM&gt;";
print"&lt;/HTML&gt;";

mysql_free_result($rsReg);
?&gt;</font id=code></pre id=code>

Replies

Replied 27 Jun 2003 03:02:30
27 Jun 2003 03:02:30 waleed ware replied:
This is the database dump , if anyone would like to try it :

# phpMyAdmin MySQL-Dump
# version 2.5.0
# www.phpmyadmin.net/ (download page)
#
# Host: localhost
# Generation Time: Jun 27, 2003 at 04:05 AM
# Server version: 3.23.49
# PHP Version: 4.3.1
# Database : `logbook`
# --------------------------------------------------------

#
# Table structure for table `airfield`
#
# Creation: Feb 14, 2003 at 03:08 PM
# Last update: Feb 14, 2003 at 03:08 PM
#

CREATE TABLE `airfield` (
`A-FID` int(15) unsigned NOT NULL auto_increment,
`3-letter` varchar(20) NOT NULL default '0',
`4-letter` varchar(20) NOT NULL default '0',
PRIMARY KEY (`A-FID`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

#
# Dumping data for table `airfield`
#

# --------------------------------------------------------

#
# Table structure for table `areg`
#
# Creation: Feb 17, 2003 at 09:40 PM
# Last update: Jun 08, 2003 at 12:32 AM
#

CREATE TABLE `areg` (
`areg` varchar(20) NOT NULL default '',
`atypeID` int(15) NOT NULL default '0',
`aregID` int(15) NOT NULL auto_increment,
PRIMARY KEY (`aregID`)
) TYPE=MyISAM AUTO_INCREMENT=10 ;

#
# Dumping data for table `areg`
#

INSERT INTO `areg` (`areg`, `atypeID`, `aregID`) VALUES ('9K-ANA', 1, 1);
INSERT INTO `areg` (`areg`, `atypeID`, `aregID`) VALUES ('9K-ANB', 1, 2);
INSERT INTO `areg` (`areg`, `atypeID`, `aregID`) VALUES ('9K-ANC', 1, 3);
INSERT INTO `areg` (`areg`, `atypeID`, `aregID`) VALUES ('9K-AND', 1, 4);
INSERT INTO `areg` (`areg`, `atypeID`, `aregID`) VALUES ('9K-AKA', 2, 5);
INSERT INTO `areg` (`areg`, `atypeID`, `aregID`) VALUES ('9K-AKB', 2, 6);
INSERT INTO `areg` (`areg`, `atypeID`, `aregID`) VALUES ('9K-AKC', 2, 7);
INSERT INTO `areg` (`areg`, `atypeID`, `aregID`) VALUES ('9K-AOA', 3, 8);
INSERT INTO `areg` (`areg`, `atypeID`, `aregID`) VALUES ('9K-AOB', 3, 9);
# --------------------------------------------------------

#
# Table structure for table `atype`
#
# Creation: Jun 21, 2003 at 11:12 PM
# Last update: Jun 21, 2003 at 11:13 PM
#

CREATE TABLE `atype` (
`atypeID` int(15) unsigned NOT NULL auto_increment,
`atype` varchar(50) NOT NULL default '0',
PRIMARY KEY (`atypeID`)
) TYPE=MyISAM AUTO_INCREMENT=10 ;

#
# Dumping data for table `atype`
#

INSERT INTO `atype` (`atypeID`, `atype`) VALUES (1, 'A 340-300');
INSERT INTO `atype` (`atypeID`, `atype`) VALUES (2, 'A320');
INSERT INTO `atype` (`atypeID`, `atype`) VALUES (3, 'B777');
INSERT INTO `atype` (`atypeID`, `atype`) VALUES (4, 'B747-200');
INSERT INTO `atype` (`atypeID`, `atype`) VALUES (5, 'B747-400');
INSERT INTO `atype` (`atypeID`, `atype`) VALUES (6, 'A310');
INSERT INTO `atype` (`atypeID`, `atype`) VALUES (7, 'A300');
INSERT INTO `atype` (`atypeID`, `atype`) VALUES (8, 'GV');
INSERT INTO `atype` (`atypeID`, `atype`) VALUES (9, '&lt;--Select Aircraft Type--&gt;');
# --------------------------------------------------------

#
# Table structure for table `logbook`
#
# Creation: Feb 14, 2003 at 09:15 PM
# Last update: Feb 14, 2003 at 09:15 PM
#

CREATE TABLE `logbook` (
`FltID` int(15) unsigned NOT NULL auto_increment,
`A-TypeID` int(15) NOT NULL default '0',
`A-RegID` int(15) NOT NULL default '0',
`FltDate` timestamp(14) NOT NULL,
`A-FID` int(15) NOT NULL default '0',
`Coff` varchar(20) NOT NULL default '',
`Con` varchar(20) NOT NULL default '',
`Total` int(50) NOT NULL default '0',
PRIMARY KEY (`FltID`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

#
# Dumping data for table `logbook`
#


Replied 27 Jun 2003 03:06:15
27 Jun 2003 03:06:15 waleed ware replied:
Oh and another thing , if you have a simpler way to create a dependent dynamic dropdowns using PHP/MySQL ( with Javascript) in Dreamweaver MX , please let me know , or at least point me to somewhere where it shows how to.

Thanks and sorry for the lenghty question , as I wanted to show you where the problems are.

thanks
Replied 26 Dec 2005 02:00:44
26 Dec 2005 02:00:44 Steve Lawrie replied:
I don't know if this is a help. I also need a dependent dropdown menu and have been trying for days to massage this into my site.

forums.devnetwork.net/viewtopic.php?t=29084
Replied 01 Feb 2006 15:42:22
01 Feb 2006 15:42:22 P O replied:
Hi!

I used your script but have taken away one of the two dropdownlists. (Didnt need it).

When I now have released the website I got this error.
Maybe I have missed to taken away something.

Please help.
//PO

This is the error message:
Parse error: syntax error, unexpected T_VAR in /secret/ap_profiles_aaa.php on line 24

---------------------------------------
This is the script changed to fit our service:


&lt;?php require_once('Connections/conn_secret.php'); ?&gt;
&lt;?php
mysql_select_db($database_conn_secret_airports, $conn_secret_airports);
$query_rs_flighttourNames = "SELECT tbl_airport.airportID, tbl_airport.airportName, tbl_airport.icaoDesignator FROM tbl_airport ORDER BY tbl_airport.airportName";
$rs_flighttourNames = mysql_query($query_rs_flighttourNames, $conn_secret_airports) or die(mysql_error());
$row_rs_flighttourNames = mysql_fetch_assoc($rs_flighttourNames);
$totalRows_rs_flighttourNames = mysql_num_rows($rs_flighttourNames);
?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;script language="JavaScript"&gt;
&lt;!--

var arrDynaList = new Array();
var arrDL1 = new Array();

arrDL1[1] = "atype"; // Name of parent list box
arrDL1[2] = "form1"; // Name of form containing parent list box
arrDL1[3] = arrDynaList;

&lt;%
var txtDynaListRelation = "airportID" // Name of recordset field relating to parent
var varDynaList = -1;
var varMaxWidth = "10";
var varCheckGroup = oDynaListRS.Fields.Item(txtDynaListRelation).Value;
var varCheckLength = 0;
var varMaxLength = 0;

while (!oDynaListRS.EOF){

if (varCheckGroup != oDynaListRS.Fields.Item(txtDynaListRelation).Value) {
varMaxLength = Math.max(varCheckLength, varMaxLength)
varCheckLength = 0;
}
%&gt;
arrDynaList[&lt;%=(varDynaList+1)%&gt;] = "&lt;%=(oDynaListRS.Fields.Item(txtDynaListRelation).Value)%&gt;";
&lt;%

if (oDynaListRS.Fields.Item(txtDynaListLabel).Value.length &gt; varMaxWidth.length) {
varMaxWidth = oDynaListRS.Fields.Item(txtDynaListLabel).Value;
}
varCheckLength = varCheckLength + 1;
varDynaList = varDynaList + 3;
oDynaListRS.MoveNext();
}
varMaxLength = Math.max(varCheckLength, varMaxLength)
%&gt;


//--&gt;
&lt;/script&gt;

&lt;!-- End of object/array definitions, beginning of generic functions --&gt;

&lt;script language="JavaScript"&gt;
&lt;!--
function setDynaList(arrDL){

var oList1 = document.forms[arrDL[2]].elements[arrDL[1]]
var arrList = arrDL[3]

clearDynaList(oList2);

if (oList1.selectedIndex == -1){
oList1.selectedIndex = 0;
}

populateDynaList(oList2, oList1[oList1.selectedIndex].value, arrList);
return true;
}

function clearDynaList(oList){

for (var i = oList.options.length; i &gt;= 0; i--){
oList.options[i] = null;
}

oList.selectedIndex = -1;
}

function populateDynaList(oList, nIndex, aArray){

for (var i = 0; i &lt; aArray.length; i= i + 3){
if (aArray[i] == nIndex){
oList.options[oList.options.length] = new Option(aArray[i + 1], aArray[i + 2]);
}
}

if (oList.options.length == 0){
oList.options[oList.options.length] = new Option("[none available]",0);
}

oList.selectedIndex = 0;
}

function MM_callJS(jsStr) { //v2.0
return eval(jsStr)
}
//--&gt;
&lt;/script&gt;
&lt;link href="corporatestyle.css" rel="stylesheet" type="text/css" /&gt;
&lt;/head&gt;
&lt;body onLoad="MM_callJS('setDynaList(arrDL1)')"&gt;
&lt;form name="form1" method="post" action=""&gt;
&lt;?php do { ?&gt;
&lt;select name="atype" class="searchform" id="atype" onChange="MM_callJS('setDynaList(arrDL1)')"&gt;
&lt;option value="&lt;?php echo $row_rs_flighttourNames['airportID']?&gt;"&gt;&lt;/option&gt;
&lt;?php
} while ($row_rs_flighttourNames = mysql_fetch_assoc($rs_flighttourNames));
$rows = mysql_num_rows($rs_flighttourNames);
if($rows &gt; 0) {
mysql_data_seek($rs_flighttourNames, 0);
$row_rs_flighttourNames = mysql_fetch_assoc($rs_flighttourNames);
}
?&gt;
&lt;/select&gt;
&lt;?php } while ($row_rs_flighttourNames = mysql_fetch_assoc($rs_flighttourNames)); ?&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?php
mysql_free_result($rs_flighttourNames);
?&gt;
Replied 01 Feb 2006 15:52:40
01 Feb 2006 15:52:40 P O replied:
Hi again!

Posted the question above to the wrong topic. Too many topics up now.
Sorry if I interrupted someone.<img src=../images/dmxzone/forum/icon_smile_blush.gif border=0 align=middle>

Thanks anyway!

Reply to this topic