Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 6th, 2004, 03:20 AM
Registered User
 
Join Date: Nov 2004
Location: , , Canada.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing php array values to javascript array

I wrote a php and javascript to pull data from tables (term_hierarchy, term_data, vocabulary) and populate it (without page refresh) on three dropdown boxes.

When i select dropdown 1 say "continent" in my case, i would like to have countries listed in dropdown2 and by selecting country in dropdown 2, i would like to have cities listed in the third dropdown.

Upon execution the continents are getting repeated and the countries and cities are not populated correctly.


suggestions please .....

i have enclosed php+javascript code in addition to mysql database scripts with values

php & javascript code ---->

<?
$fields = array();
$fields["continent"]=array();
$fields["country"]=array();
$fields["cities"]=array();

@mysql_connect("myhost","myuser","mypass") or die("Unable to Connect: ".@mysql_error());
@mysql_select_db("mysql") or die("Unable to select DB: ".@mysql_error());
$result = @mysql_query(" select v.name as vocabulary, t1.name as continent, t2.name as country, t3.name as provinces, t4.name as cities from
(((((((term_hierarchy th1 LEFT OUTER JOIN term_hierarchy th2 ON th1.tid = th2.parent)
LEFT OUTER JOIN term_hierarchy th3 ON th2.tid = th3.parent)
LEFT OUTER JOIN term_hierarchy th4 ON th3.tid = th4.parent)
LEFT OUTER JOIN term_data t1 ON th1.tid = t1.tid)
LEFT OUTER JOIN term_data t2 ON th2.tid = t2.tid)
LEFT OUTER JOIN term_data t3 ON th3.tid = t3.tid)
LEFT OUTER JOIN term_data t4 ON th4.tid = t4.tid)
LEFT OUTER JOIN vocabulary v ON t1.vid = v.vid
where t1.vid = 3 and th1.parent = 0") or die("Unable to query DB: ".@mysql_error());

while ($node = @mysql_fetch_array($result)) {
array_push($fields["continent"], $node["continent"]);
array_push($fields["country"], $node["country"]);
array_push($fields["cities"], $node["cities"]);
}

?>

<html>
<head>
<title>My Title</title>
</head>
<body>


<script language="JavaScript">
var a_continent = new Array();
var a_country = new Array();
var a_cities = new Array();
<?
for ($i=0; $i< count($fields['continent']); $i++) {
echo 'a_continent['.$i.']= "'. $fields['continent'][$i] .'";';
}
for ($i=0; $i< count($fields['country']); $i++) {
echo 'a_country['.$i.']= "'. $fields['country'][$i] .'";';
}
for ($i=0; $i< count($fields['cities']); $i++) {
echo 'a_cities['.$i.']= "'. $fields['cities'][$i] .'";';
}
?>


var browser = navigator.appName;
//in function below, "type" must be
//0 to set values for continent field
//1 to set values for country field
//2 to set values for cities filed
function setValues(type)
{
if(type == 0)
{
for(i=0; i<a_continent.length; i++)
{
var optionText = a_continent[i];
var optionValue = optionText;
document.myForm.continents.options[i] = new Option(optionText, optionValue);
}
}
else if(type == 1)
{
//remove the previous options
for(i=0; i<document.myForm.countries.options.length; i++)
{
if(browser == "Microsoft Internet Explorer")
document.myForm.countries.options.remove(0);
else
document.myForm.countries.options[0] = null;
}
//fill the new options
var selectedContinent = document.myForm.continents.selectedIndex;
for(i=0; i<a_country[selectedContinent].length; i++)
{
var optionText = a_country[selectedContinent][i];
var optionValue = optionText;
document.myForm.countries.options[i] = new Option(optionText, optionValue);
}
}
else if(type == 2)
{
//remove the previous options
for(i=0; i<document.myForm.cities.options.length; i++)
{
if(browser == "Microsoft Internet Explorer")
document.myForm.cities.options.remove(0);
else
document.myForm.cities.options[0] = null;
}

//fill the new options
var selectedContinent = document.myForm.continents.selectedIndex;
var selectedCountry = document.myForm.countries.selectedIndex;
for(i=0; i<a_cities[selectedContinent][selectedCountry].length; i++)
{
var optionText = a_cities[selectedContinent][selectedCountry][i];
var optionValue = optionText;
document.myForm.cities.options[i] = new Option(optionText, optionValue);
}

}
}

</script>

<body onLoad="setValues(0), setValues(1), setValues(2)">

<FORM name="myForm" id="myForm">
Continents
<select name="continents" size=1 onChange="setValues(1), setValues(2)">
<option><----------------></option>
</select><br><br><br><br><br><br><Br>
Countries
<select name="countries" size=1 onChange="setValues(2)">
<option><----------------></option>
</select><br><br><br><br><br><br><Br>
Cities
<select name="cities" size=1>
<option><----------------></option>
</select>
</form>
</body>
</html>

=========================

Mysql database script....


CREATE TABLE `term_data` (
`tid` int(10) unsigned NOT NULL auto_increment,
`vid` int(10) unsigned NOT NULL default '0',
`name` varchar(255) NOT NULL default '',
`description` longtext,
`weight` tinyint(4) NOT NULL default '0',
PRIMARY KEY (`tid`),
KEY `vid` (`vid`)
) TYPE=MyISAM AUTO_INCREMENT=43 ;

#
# Dumping data for table `term_data`
#

INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (18, 3, 'North America', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (19, 3, 'Europe', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (20, 3, 'Asia', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (21, 3, 'Japan', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (22, 3, 'China', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (23, 3, 'Tokyo', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (24, 3, 'Nagasaki', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (26, 3, 'Britain', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (27, 3, 'France', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (28, 3, 'London', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (29, 3, 'East London', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (30, 3, 'Canada', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (31, 3, 'United States', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (32, 3, 'California', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (33, 3, 'New York', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (34, 3, 'Los Angeles', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (35, 3, 'San Francisco', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (36, 3, 'New York City', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (37, 3, 'Ontario', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (39, 3, 'East Village', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (40, 3, 'Vancouver', '', 0);
INSERT INTO `term_data` (`tid`, `vid`, `name`, `description`, `weight`) VALUES (41, 3, 'Toronto', '', 0);
# --------------------------------------------------------

#
# Table structure for table `term_hierarchy`
#
# Creation: Oct 30, 2004 at 12:47 PM
# Last update: Nov 04, 2004 at 05:01 PM
#

CREATE TABLE `term_hierarchy` (
`tid` int(10) unsigned NOT NULL default '0',
`parent` int(10) unsigned NOT NULL default '0',
KEY `tid` (`tid`),
KEY `parent` (`parent`)
) TYPE=MyISAM;

#
# Dumping data for table `term_hierarchy`
#

INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (11, Cool;
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (10, 7);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (9, 7);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (8, 0);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (5, 0);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (6, 0);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (7, 0);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (12, Cool;
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (13, 6);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (14, 6);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (15, 5);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (16, 5);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (17, 5);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (18, 0);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (19, 0);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (20, 0);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (21, 20);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (22, 20);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (23, 21);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (24, 21);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (26, 19);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (27, 19);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (28, 26);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (29, 2Cool;
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (30, 1Cool;
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (31, 1Cool;
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (32, 31);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (33, 31);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (34, 32);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (35, 32);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (36, 33);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (37, 30);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (39, 36);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (40, 3Cool;
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (41, 37);
INSERT INTO `term_hierarchy` (`tid`, `parent`) VALUES (1, 0);
# --------------------------------------------------------


CREATE TABLE `vocabulary` (
`vid` int(10) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`description` longtext,
`help` varchar(255) NOT NULL default '',
`relations` tinyint(3) unsigned NOT NULL default '0',
`hierarchy` tinyint(3) unsigned NOT NULL default '0',
`multiple` tinyint(3) unsigned NOT NULL default '0',
`required` tinyint(3) unsigned NOT NULL default '0',
`nodes` longtext,
`weight` tinyint(4) NOT NULL default '0',
PRIMARY KEY (`vid`)
) TYPE=MyISAM AUTO_INCREMENT=4 ;

#
# Dumping data for table `vocabulary`
#

INSERT INTO `vocabulary` (`vid`, `name`, `description`, `help`, `relations`, `hierarchy`, `multiple`, `required`, `nodes`, `weight`) VALUES (3, 'Geography', 'Geographic Locations', '', 1, 0, 0, 1, 'blog,image,page,story', 0);


krishna
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing Multidimensional Array Values back to form stitch Classic ASP Databases 2 June 21st, 2013 01:59 PM
passing parameter array Maryann BOOK: Access 2003 VBA Programmer's Reference 0 December 4th, 2006 10:56 AM
Passing a Array in of no specifc type.. John Pennington Pro VB 6 1 September 27th, 2006 05:04 PM
Passing an array to a Sub or Function donrafeal Access VBA 2 May 11th, 2006 09:39 AM
Passing an array in a Sub krunch97 VB.NET 2002/2003 Basics 1 May 4th, 2004 01:29 PM



All times are GMT -4. The time now is 01:54 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.