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

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 January 1st, 2004, 12:19 PM
xander xander is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Spain
Posts: 5 xander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question subselect

OK guys, this is quite a problem I have here, so let me explain the situation.
I have two tables in one database. One of the tables is for countries and the other is for towns. Both tables have one column in common which is the countryid and the code. The countryid is the id of the country eg, England = 657 (id) and the code is what tells us that a town belongs to a which country, so if the town is london the code will be 657.

OK so I have two databases countries_en and towns_en
in countries_en I have two columns, country and countryid
and in towns_en I have three: town, town id and code

Now here is the problem: I want to create two drop down tables so that when I choose a country in the first table the list in the second table changes depending on which country I have chosen.
So if I choose England on the fist table, the second table will only have the towns of that country eg london, pouthampton, portsmouth etc...

I hope someone has the brains to figure this one out cause I have tried just about everything. I'm not putting any code not to confuse anyone. You have thh problem as I have it.

WHAT IS THE PHP CODE I MUST USE

version 4

If anyone dares, thanks in advance

Xander

Reply With Quote
  #2  
Old January 1st, 2004, 04:19 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
It's not a subselect you need. To build the country box, just select countries from the database and build the box with the country id as the value and the name as the label. Default value is "Select a country." Build the initial town box with only a "Select a town" option.

Onchange for the country box, submit the form, passing the country id. Use this id to select the appropriate towns and build the town box appropriately. When rebuilding the country box this time around, make sure the code for the country passed as a parameter is selected.

In pseudocode:

PHP Code:
print "<select name=\"town\">\n\t<option value="">Select a town</option>\n";

if(
$_GET["country_id"]){
    
//Select from town db where code=$_GET["country_id"]
    //For each result, print <option value="$town_id">$town</option>
}

print 
"</select>\n"

Reply With Quote
  #3  
Old January 1st, 2004, 09:25 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
From a users perspective, you want the city droplist to auto-populate when you choose a country, correct?

Here's how i would approach it... use dhouston's method to populate the country box... add an onchange event to the select box which submits the form to itself... then in php, test if the form variable for country exists... then populate the city select with the query results based on the data passed... [pseudo-example: SELECT * FROM city, country WHERE country = city AND country = POST_DATA]

i hope i'm being clear... i dont feel like writing the code out for you, although i hope i'm clear enough to give you that push you need to get the ol' wheels turning...

Reply With Quote
  #4  
Old January 2nd, 2004, 06:49 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
I think we're saying pretty much the same thing, right MadCowDzz? On rereading my post today, I can't help thinking I wasn't very clear. Hopefully your post clarified mine a bit.

Reply With Quote
  #5  
Old January 2nd, 2004, 08:11 AM
xander xander is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Spain
Posts: 5 xander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I got the countries displaying and working fine but I don’t know how to make a different list of towns come up depending on which country I select. The code is the following:


PHP Code:
</TD>
    </
TR>

    <
TR>
      <
TD valign="top" width="225">
      <
B><?php echo TEXT_COUNTRY;?>:</B></FONT>
      </TD>
      <TD colspan="4" width="525">
        <SELECT name="country" size="1">
<?php
          $country_query
=bx_db_query("select * from ".$bx_table_prefix."country".$bx_table_lng."");
          while (
$country_result=bx_db_fetch_array($country_query)  )
          {
          echo 
'<option value="'.$country_result['countryid'].'"';
          if (
$job_result['countryid']==$country_result['countryid']) {echo " selected";}
          echo 
'>'.$country_result['country'].'</option>';
          }
          
?>




        </SELECT>
      </TD>
    </TR>
    <TR>
      <TD valign="top" width="225">
      <B><?php echo TEXT_CITY;?>:</B></FONT>
      </TD>
      <TD colspan="4" width="525">
       <SELECT name="postcode" size="1">


        <?php





//This is where I need to put the code for the town, by doing the same as the above I get the list of all the cities but we all know that that is not what I want.




          
?>






      </TD>
    </TR> 


thanks

Xander

Reply With Quote
  #6  
Old January 2nd, 2004, 08:55 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
PHP Code:
if($_GET["country"]){ //Or $_POST, if you're using that
    //Do a query on your town table where the country id for the given town is $_GET["country"] and print an option for each result.


Reply With Quote
  #7  
Old January 2nd, 2004, 11:06 AM
xander xander is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Spain
Posts: 5 xander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Still no luck, maybe its because I didn't really understand what you were trying to say. You must know that I am very new to php and mysql. I was hopeing you could write me an exact piece of code that I could put in to try it out. I hope I'm not asking for too much.

thanks again

Reply With Quote
  #8  
Old January 2nd, 2004, 03:57 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
It's just a matter of piecing together two snippets of code already provided above, roughly as follows. You'll need to check all the column names, etc.

PHP Code:
echo "<select name=\"town\">\n";

if(
$_GET["country"]){
$town_query=bx_db_query("select * from ".$bx_table_prefix."towns_".$bx_table_lng." WHERE code=" intval($_GET["country"]));
while  (
$town_result=bx_db_fetch_array($town_query))
    {                    
        echo 
'\t<option value="'.$town_result['townid'].'"';
        echo 
'>'.$town_result['town'].'</option>\n';
    } 
}

echo 
"</select>\n"

Reply With Quote
  #9  
Old January 3rd, 2004, 07:51 AM
xander xander is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Spain
Posts: 5 xander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I tried everything, still nothing. The code makes no difference, there isn't even a list displayed, just an empty table. I don't know what more I can do but thanks for your help anyway

Reply With Quote
  #10  
Old January 3rd, 2004, 10:56 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
Try running your queries manually (at the command line or through a tool like MysqlAdmin) to make sure they're ok. It could be that your code's fine but your queries are wrong.

Reply With Quote
  #11  
Old January 3rd, 2004, 12:54 PM
xander xander is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Spain
Posts: 5 xander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I tried that too, with phpmyadmin, the queries are fine, this is the most annoying problem I have come across and all I want to do is what madcowdzz says in his first line and be able to save the settings for the user. Guess this stuff is more complicated than I imagined. I'm getting on with other stuff but it would be good if I could solve this problem once and for all. Thanks again dhouston.

Xander

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > subselect


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 |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT