General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 17th, 2003, 02:41 AM
kupilot kupilot is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 8 kupilot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy dependant dropdown lists , please help find the problem!

Hi
I found this javascript which creates a dependendant dropdown lists.
That is if you select an item from the one dropdown list the other dropdown
lists updates with the option relating to that item.
An example : if u have 2 dropdownlist (lets call drop1 country and drop2
city) if you select a country from country dropdown the city dropdown
updates with the available cities for that country.

So far so good.

Now what i wanted to do is populate the country dropdown from a mysql
database using php , which worked.

here is the javascript i found on the internet:
<script>
// Define the values for your drop downs here
// Note: If you want different text than value you can also specify text1_0
etc.
value1_0='#';
value1_1='1';
value1_2='2';
value1_3='3';
text1_0='Pick a city';
text1_1='Copenhagen';
text1_2='Odense';
text1_3='Skagen';

value2_0='#';
text2_0='Pick a city';
value2_1='1';
value2_2='2';
value2_3='3';
text2_1='Stockholm';
text2_2='Gothenburg';
text2_3='Kiruna';

// Continue with value3_0 etc...
// Leave the rest of the script as is

function update_dropdowns(mother)
{
child=0; document.myform.child.options.length=1;
while(eval('window.value'+mother+'_'+child)||eval( 'window.text'+mother+'_'+c
hild)){
document.myform.child.options.length=child+1;
id=mother+'_'+child;
if (!eval('window.value'+id)) {eval('value'+mother+'_'+child+'=text'+id)}
if (!eval('window.text'+id)) {eval('text'+mother+'=value'+id)}
document.myform.child.options[child].value=eval('value'+id);
document.myform.child.options[child].text=eval('text'+id);
child++;
}
}
</script>

Now i want this part not to be hardcoded , instead pulled from a Mysql
database using php:
// Define the values for your drop downs here
// Note: If you want different text than value you can also specify text1_0
etc.
value1_0='#';
value1_1='1';
value1_2='2';
value1_3='3';
text1_0='Pick a city';
text1_1='Copenhagen';
text1_2='Odense';
text1_3='Skagen';

value2_0='#';
text2_0='Pick a city';
value2_1='1';
value2_2='2';
value2_3='3';
text2_1='Stockholm';
text2_2='Gothenburg';
text2_3='Kiruna';

// Continue with value3_0 etc...
This is what i tried but didn`t work , can anyone look at it and help to
find the problem , ie find out why the child dropdown doesn`t update
properly as it worked with the hardcoding of their text & values:
I put this instead of the above:
PHP Code:
<?php
mysql_select_db
($database_connKAC$connKAC);
$query_rssub "SELECT * FROM tipsubcat, tipcat WHERE tipsubcat.catid =
tipcat.catid ORDER BY tipsubcat.catid"
;
$rssub mysql_query($query_rssub$connKAC) or die(mysql_error());
$row_rssub mysql_fetch_assoc($rssub);
$totalRows_rssub mysql_num_rows($rssub);
while (
$subval mysql_fetch_assoc($rssub)){
print (
"value".$subval['catid']."_0='Pick A
City';\ntext"
.$subval['catid']."_0='Pick A City';\n");
print
(
"value".$subval['catid']."_".$subval['subid']."='".$subval['subid']."';\n"
."text".$subval['catid']."_".$subval['subid']."='".$subval['subname']."';\n"
);
}
?>

Thanks for any further help

Reply With Quote
  #2  
Old July 17th, 2003, 08:04 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
What's the output of your code? Does it print the javascript correctly? One option, incidentally, might be to skip the javascript and have the county box submit the form on change and populate the city box based on its value. Of course, you'd have to remember and fill in all other form values as well on the reload, but that's not too big a deal.

Reply With Quote
  #3  
Old July 17th, 2003, 11:10 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
Yes, you do need to post your output in order for someone to help you.
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #4  
Old July 17th, 2003, 11:51 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
Kupilot, have you read the forum rules?
http://www.devarticles.com/forum/an...hp?s=&forumid=4

Please see section (3)

Reply With Quote
  #5  
Old July 18th, 2003, 12:05 AM
kupilot kupilot is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 8 kupilot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
laidbak , i`ve read the rules , sorry that you think that i`m waisting your time , i`ve attached both the code and the IE output of it , if you think you can help , then please do , i think my question should be simple to answer , as i`m looking for a way to populate a javascript array with data from mysql database using php , i`m trying to solve it myself , but can`t find where the problem is , there is no error from IE , just that the second dropdown box is not correctly & dynamiclly populated.

Hope after revising my post someone would help , and not teach the rule on how to post
Attached Files
File Type: txt drop1.txt (5.7 KB, 267 views)

Reply With Quote
  #6  
Old July 18th, 2003, 01:26 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
You are producing an invalid set of data:
I took your variable set and exported it to a text file, then imported it into excel to sort and get a clear view of the results...
Here are the first couple results:
Code:
text1	0='Pick A City';
text1	5='98';
text2	0='Pick A City';
text2	0='Pick A City';
text2	6='Internet Explorer';
text2	7='Outlook Express';
value1	0='Pick A City';
value1	5='5';
value2	0='Pick A City';
value2	0='Pick A City';
value2	6='6';
value2	7='7';

Although you do have a valid set of data, you are missing one fundamental part of your application. You are going from text1_0 to text1_5, and the same for the value match for this variable.

If you take the html your application produced and put it into its own html file, try changing this:

value1_0='Pick A City';
text1_0='Pick A City';
value1_5='5';
text1_5='98';


To this:

value1_0='Pick A City';
text1_0='Pick A City';
value1_1='5';
text1_1='98';

for clarity you can even add these lines:

value1_2='6';
text1_2='2000';

See that it works.
In summary, all you need to do is make sure your query results produce a proper sequencial set of indexes.

Reply With Quote
  #7  
Old July 18th, 2003, 03:27 AM
kupilot kupilot is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 8 kupilot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi laidbak

Thanks for the reply.

So what you`re saying the problem is with the logic of the query or the sort of the out.

What it should output is like this:

I have 2 tables in the database , table1 is categories with catid/name fields , table 2 is subcategories with subcatid/subname/catid fields. They are joind by catid fields.(as you can see in the query).

Now the javascript array should look like this:

value1_0='Pick A Country';
value1_1='Bangkok';
value1-2='singapore';
text1_0='Pick A country';
text1_1="Bangkok';
text1_2="singapore';
and so on for category 1 as the idea is that the value and text format is as follows:

valuecatid_subcatid=string in the database field;
textcatid_subcatid=string in the database field;

The do/while loop tries to do that for me as you may see in the code, but i think the sorting of the array needs to be refind.

No who would you do this task , I don`t know how to.

I tried to lookup in the PHP/Mysql manuals and all over the forums in the internet , couldn`t find a solution.

I`d like to know that because i want also to write a dynamic news ticker , where the news articles are pulled from mysql database , and put in an javascript array like this:
msg[0]=msg1;
msg[1]=msg2; and so on.

Shall I change the do/while loop in a for loop, or shall i put it inside a for loop , and how would i do that , especially if you this category/subcategory relation.

Thanks in advance.

Reply With Quote
  #8  
Old July 18th, 2003, 08:05 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Couldn't you just change these lines:

PHP Code:
print

(
"value".$subval['catid']."_".$subval['subid']."='".$subval['subid']."';\n"

."text".$subval['catid']."_".$subval['subid']."='".$subval['subname']."';\n"

); 


to

PHP Code:
print

(
"value".$subval['catid']."_".$count."='".$subval['subid']."';\n"

."text".$subval['catid']."_".$count."='".$subval['subname']."';\n"

);

$count++; 


or something similar so that you get sequential values?

Reply With Quote
  #9  
Old July 18th, 2003, 12:37 PM
kupilot kupilot is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 8 kupilot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by dhouston
Couldn't you just change these lines:

PHP Code:
print

(
"value".$subval['catid']."_".$subval['subid']."='".$subval['subid']."';\n"

."text".$subval['catid']."_".$subval['subid']."='".$subval['subname']."';\n"

); 


to

PHP Code:
print

(
"value".$subval['catid']."_".$count."='".$subval['subid']."';\n"

."text".$subval['catid']."_".$count."='".$subval['subname']."';\n"

);

$count++; 


or something similar so that you get sequential values?


Sorry dhouston that didn`t help.
There must be a different way to populate any javascript array (assoc/multidimensional or whatever) from mysql database using php. I`m still trying to figure it out though.

Reply With Quote
  #10  
Old July 18th, 2003, 02:51 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Try something like the following code.

PHP Code:
<?php

//Just creating a fake data set. Not sure whether or not this corresponds 
//to what you've got, but it should be something you can work with.
$rows=array(
    array(
"catid"=>3,"subid"=>6,"subname"=>"Sri Lanka"),
    array(
"catid"=>3,"subid"=>8,"subname"=>"Bangladesh"),
    array(
"catid"=>3,"subid"=>9,"subname"=>"China"),

    array(
"catid"=>5,"subid"=>1,"subname"=>"Word"),
    array(
"catid"=>5,"subid"=>3,"subname"=>"Power Point"),
    array(
"catid"=>5,"subid"=>23,"subname"=>"Internet Explorer")
);

//Set some counters.
$fields=0;
$elms=0;
$current="";
$output="";

//For each row in the record set...
foreach($rows as $subval){
    
//If the catid differs from $current, set $current to that value, increment $fields, 
    //reset $elms to 0, and print a break in the code.

    
if($subval["catid"] != $current){
        
$current=$subval["catid"];
        
$fields++;
        
$elms=0;
        
$output .= "\n";
    }

    
//Now print the js lines based on the two counters we've got going and increment $elms.
    
$output .= "value" $fields "_" $elms "='" $subval["subid"] . "';\n";
    
$output .= "text" $fields "_" $elms "='" $subval["subname"] . "';\n";

    
$elms++;
}
 
//Print the output. I'm converting newlines to <br> tags for display purposes.
print preg_replace("/\n/","<br>",$output);

?>


The output of this when I run it is as follows:

Code:
value1_0='6';
text1_0='Sri Lanka';
value1_1='8';
text1_1='Bangladesh';
value1_2='9';
text1_2='China';

value2_0='1';
text2_0='Word';
value2_1='3';
text2_1='Power Point';
value2_2='23';
text2_2='Internet Explorer';


If that's not exactly what you need, you can see the mechanism I've used to generate that output and should be able to adapt it according to your needs. Hope this helps.

Last edited by dhouston : July 18th, 2003 at 02:54 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > dependant dropdown lists , please help find the problem!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |