Jump to content

Archived

This topic is now archived and is closed to further replies.

nestorvaldez

Filter Data

Recommended Posts

I need some Help.
I want to filter my data in two combo box field, retreiving the data form my mysql DB.
Ex.
I have three DB : country, city, customer; city is relationed with country; and customer with both
so I want that when I select on country in the Customer Form the script could show/filter only the cities that belong to this country..

COUNTRY         
- id
-countryname

CITY         
- id
- cityname
- country

CUSTUMER
- id
- name
- country
- city
                 

Share this post


Link to post
Share on other sites
ok. so if the country table has an ide and city has an aread for the country, then ur set.

Simply call the city by country:
[code]
// let's just say the country id is 2 //
$country_id = 2;
$sql = "SELECT * FROM CITY WHERE country='" . $cid . "'";
$results = mysql_query($sql);

while($row = mysql_fetch_rows($results)){

// HERES UR CITIES

}

[/code]

Share this post


Link to post
Share on other sites
Im getting problem doing it..
The problem is, it is in the new record form Im try to register a new one.. So I dont have it the code in the DB, yet.. My problem is that I cant make refresh the page in order show the city in my combo, depending on the country I selected.. I mean the first field is the country and the next one is the City, then I want that when I select the One country and go out from this field to the next it make the refresh or what ever and show me in the next field (the Cities combo) just the cities from the country I selected...

This is my code to select the country from the DB

<select size="1" name="country"  tabindex="13">
  <?php
          $query = mysql_query("SELECT PAI_ID, PAI_NOMBRE FROM pais");
    while ($row = mysql_fetch_array($query)) {
    $pid=$row['PAI_ID'];
    $pnombre=$row['PAI_NOMBRE'];
    ?>
    <option value="<?=$pid?>" <?//=$selected?>><?=$pnombre?></option>
    <?
    }
    ?>
</select>


and this is to select the City:

  <select size="1" name="city" tabindex="12">
  <?php
          $query = mysql_query("SELECT CIU_ID, CIU_NOMBRE FROM ciudad" WHERE CIU_PAIS=$pid);
    while ($row = mysql_fetch_array($query)) {
    $cid=$row['CIU_ID'];
    $cnombre=$row['CIU_NOMBRE'];
    ?>
    <option value="<?=$cid?>" <?//=$selected?>><?=$cnombre?></option>
    <?
    }
    ?>
</select>

How to refresh dinamically when the focus leave the Country field

Share this post


Link to post
Share on other sites
well you have a problem with your code i noticed immediatly.. this:
<option value="<?=$pid?>" <?//=$selected?>><?=$pnombre?></option>

should be more like this:
<option value="<? echo $pid; ?>" <? echo $selected; ?><? echo $pnombre; ?></option>

and you can refresh using javascript. I'm not real good with javascript but a search on google should come up with good results.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.