Jump to content

Filter Data


nestorvaldez

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
                 
Link to comment
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]
Link to comment
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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.